`

【android开发】android操作文件

阅读更多

目的:打开文件,查看文件内容,情况内容,追加内容。

1.创建android project

2.activity内容:

package com.android;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Activity12 extends Activity implements OnClickListener{

	private EditText inputEt;//编辑框
	private Button saveBtn;//保存 按钮
	private String input_text;//输入的字符串
	private OutputStream os;
	
	private TextView showText; //显示读取的内容
	private Button openTxtBtn; //open file button
	private Button cleanTxtBtn;//clean file
	private String text_output; 
	private InputStream is;
	private byte[] b;
	
	private final String FILE = "filetext.txt";
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
    }
	
	/**
	 * 设置显示view
	 * @param layoutId
	 */
	private void setLayout(int layoutId) {
		setContentView(layoutId);
	}
	
	/**
	 * 各种布局下,得到view
	 * @param mainOpen
	 */
	private void initUI(String mainOpen) {
		if (mainOpen.equals("main")) {
			inputEt = (EditText)findViewById(R.id.edit_txt);
			saveBtn = (Button)findViewById(R.id.save);
		} else if (mainOpen.equals("open")) {
			showText = (TextView)findViewById(R.id.showTxt);
			openTxtBtn = (Button)findViewById(R.id.openTxtB);
			cleanTxtBtn = (Button)findViewById(R.id.cleanBtn);
		}
	}

	/**
	 * 不同布局下添加自己的Event
	 * @param str
	 */
	private void addEvent(String str) {
		if (str.equals("main")) {
			saveBtn.setOnClickListener(this);
		} else if (str.equals("open")) {
			openTxtBtn.setOnClickListener(this);
			cleanTxtBtn.setOnClickListener(this);
		}
	}
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.save:{
			input_text = inputEt.getText().toString();
			try {
				os = this.openFileOutput(FILE, MODE_PRIVATE);
				os.write(input_text.getBytes());
			} catch(FileNotFoundException e) {
				toast(getString(R.string.fileCloseError) + e);
			} catch(IOException e) {
				toast("文件写入失败" + e);
			} finally {
				try {
					os.close();
				} catch(Exception e) {
					toast(getString(R.string.fileCloseError) + e);
				}
			}
			inputEt.setText("");
			toast("文件保存成功!!!查看请点击Menu");
		}
		break;
		case R.id.openTxtB: {
			toast("文件打开");
			try {
				
				is = this.openFileInput(FILE);
				//is = getResources().openRawResource(R.raw.filetext);
				b = new byte[1024];
				int length = is.read(b);
				text_output = new String(b);
				setTitle("文件字数 " + length);
				showText.setText(text_output);
			} catch(FileNotFoundException e) {
				toast("文件打开失败" + e);
			} catch(IOException e) {
				toast("文件读取失败" + e);
			} finally {
				try {
					is.close();
				} catch(Exception e) {
					toast(getString(R.string.fileCloseError) + e);
				}
			}
		}
		break;
		case R.id.cleanBtn: {
			showText.setText("");
			toast(getString(R.string.clean));
		}
		break;
		default:
			break;
		}
	}

	/* 增加menu
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		
		menu.add(0, 1, 1, "Edit").setIcon(R.drawable.edit);
		menu.add(0, 2, 2, "Open").setIcon(R.drawable.open);
		menu.add(0, 3, 3, "Exit").setIcon(R.drawable.exit);
		return super.onCreateOptionsMenu(menu);
	}

	/* 点击menu item
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1: 
			setLayout(R.layout.main);
			initUI("main");
			addEvent("main");
			setTitle("请输入追加的内容:");
			toast("编辑文件");
			break;
		case 2:
			setLayout(R.layout.open);
			initUI("open");
			addEvent("open");
			toast("打开文件");
			break;
		case 3:
			finish();
			toast("Byebye");
			break;
		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
		
	}
	
	/**
	 * 提示
	 * @param str 内容
	 */
	private void toast(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
	
	
	
    
}

  3.布局文件

main.xml 追加文件内容填写页,一个editText 一个button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/hui"
    >
<EditText  
	android:id="@+id/edit_txt"
    android:layout_width="350px" 
    android:layout_height="350px" 
    />
<Button
	android:layout_height="wrap_content"
	android:id="@+id/save"
	android:text="保存"
	android:layout_width="80px"
/>
</LinearLayout>

 show.xml 首页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/hui"
    >
    <TextView  
	android:id="@+id/ss"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textSize="30px"
    android:text="粗糙文件编辑,请点击Menu" 
    />
</LinearLayout>

  open.xml 操作文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/openlayout"
    android:background="@color/hui"
    >
<TextView  
	android:id="@+id/showTxt"
    android:layout_width="314px" 
    android:layout_height="373px" 
	android:layout_x="3px"
	android:layout_y="3px"
    />
<Button
	android:layout_height="wrap_content"
	android:id="@+id/openTxtB"
	android:text="打开"
	android:layout_width="80px"
	android:layout_x="2px"
	android:layout_y="378px"
/>
<Button
	android:layout_height="wrap_content"
	android:id="@+id/cleanBtn"
	android:text="清空"
	android:layout_width="80px"
	android:layout_x="239px"
	android:layout_y="378px"
/>
</AbsoluteLayout>

 4.string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">12</string>
    <string name="fileCloseError">文件关闭失败</string>
    <string name="clean">清空</string>
    <color name = "hui">#FFFACD</color>
</resources>

 结果:

首页

 点menu

点open

点edit

点exit

 

明天继续完善。美化。。。。

byebye!!!

 

 

分享到:
评论

相关推荐

    android开发之文件操作——文件创建和文件读取

    在Android开发中,文件操作是不可或缺的一部分,无论是存储用户数据、日志信息还是应用程序的状态,都需要对文件进行创建、读取、写入等操作。本项目"android开发之文件操作——文件创建和文件读取"提供了相关的代码...

    Android文件管理器源码

    Android文件管理器(增加了文件夹复制移动,下载资源暂停删除等,以及复制过程中的可视化进程)是一个基于Android开发的应用,包含常用文件操作以及文件下载功能,文件操作包括打开文件夹和打开各类的文件(apk、avi...

    Android的文件操作

    android实际开发中遇到的关于文件操作方面的总结,以word文档形式展现出来。个人认为比较全面~

    Android开发之简单文件管理器实现方法

    本文实例讲述了Android开发之简单文件管理器实现方法。分享给大家供大家参考,具体如下: 这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件...

    Android开发入门60个小案例+源代码

    "Android开发入门60个小案例+源代码"这个资源提供了丰富的实践练习,旨在帮助初学者通过实际操作来熟悉这些概念。 首先,Android应用开发的基础是理解Activity和Intent。Activity是Android应用中的一个单一屏幕,...

    Android NDK开发指南-android.mk文件

    Android NDK 是 Android 操作系统中的一种开发工具,用于使用 C/C++ 语言编写 Android 应用程序。Android.mk 文件是 NDK 中的一个重要组件,用于描述编译系统的配置,使得开发者可以轻松地将 C/C++ 代码编译成 ...

    android开发揭秘PDF

    第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...

    安卓Android开发电子书大全 2018 (1/4)

    Android嵌入式智能操作系统是基于Linux内核和驱动的,对于HTC、华为等公司开发Android操作系统时,需要专门将Android移植到 特定硬件平台下,同时将必要的驱动进行编写及开发。 文件目录: 10个常见的_Android_新手...

    Android移动应用开发习题答案.pdf

    为了帮助开发者更好地学习和掌握 Android 移动应用开发技术,本文将提供一份详细的习题答案,涵盖 Android 开发环境搭建、模拟器创建、Android Studio 的组成结构与基本操作等多个方面。 一、Android 开发环境搭建...

    Android开发文件,android代码,android开发demo,android开发项目

    Android开发文件,android代码,android开发demo,android开发项目

    Android文件上传,文件选择器,多选

    综上所述,实现Android的文件上传和多选、单选文件功能涉及到了文件选择器的定制、文件操作、网络通信等多个方面。通过合理的设计和优化,可以提供高效、安全的文件操作体验。在实际开发中,要不断迭代和优化,确保...

    Android开发教程(完整版)

    新版Android开发教程&笔记--基础入门一.pdf 新版Android开发教程&笔记--基础入门二.pdf 新版Android开发教程&笔记三--环境搭建与解析.pdf ...新版Android开发教程+笔记十二--文件存取、数据库编程.pdf

    AndroidAPP开发入门教程.pdf

    本教程通过实践操作,带领读者一步步完成 Android APP 的开发,帮助读者快速掌握 Android APP 开发的基本技能。 知识点一: Android 开发环境搭建 * 下载和安装 JDK 和 Android SDK * 配置 IDE 增加 SDK 支持 * ...

    android txt文件保存读取操作

    测试不同的设备和Android版本,确保文件操作的兼容性。注意在Android 10(API级别29)之后,外部存储的访问方式有所改变,需要使用`getExternalFilesDir()`或`getExternalCacheDir()`方法来获取特定应用的外部存储...

    老罗android开发视频教程全集百度网盘下载

    数据储存与文件操作、对话框、通知、菜单、 LoaderManager异步加载、多线程(AsyncTask与Handler)、 百度地图等十五个模块,一共102集。 本网盘分享章节编号是按照视频更新的先后顺序编号的,具体学习可参考如下...

    android读取Excel文件(Android studio开发环境)

    首先,我们需要了解Android对文件操作的基本方法,以及如何处理Excel文件,特别是XLS格式。 在Android中,文件操作主要通过`java.io`和`android.content.Context`提供的接口来完成。对于存储在手机根目录的Excel...

    android eclispe 开发环境集成,无需安装主文件

    总结起来,"android eclipse 开发环境集成,无需安装主文件"是一个方便快捷的解决方案,它消除了手动配置Android开发环境的复杂性,使开发者能够更专注于编写和测试应用。对于那些不熟悉或不愿意处理配置问题的人来...

    Android开发教程笔记完全版 pdf

    Android开发是全球最受欢迎的移动应用开发平台之一,广泛应用于智能手机、平板电脑以及各种智能设备上。这份"Android开发教程笔记完全版"涵盖了Android开发的基础到高级主题,旨在帮助开发者全面理解并掌握Android...

Global site tag (gtag.js) - Google Analytics