java 输入输出流和File简单解析(附android中文乱码问题解决)

java的输入输出流是用来干甚么的?以下来自百度。点击打开链接

现在只有文件输出输出流和File的简单应用,以后再补大笑

输入输出不是对机器而言的

输入输出是相对于程序来说的。程序运行时是在内存中的

输入是指将硬盘或其他存储设备的数据读入内存
输出是指将内存中的数据写入存储设备
所以说输入流是用来读取的(从外面向程序里进),输出流是用来写入的(从程序里向外出)。

而他们的本质就是数据传输的媒介或者通道,表示数据的流动序列。

Java所有的I/O机制都是基于数据流进行输入输出。并且Java的I/O流提供了读写数据的标准方法。

任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。

File:

File:源码注释如下

An "abstract" representation of a file system entity identified by a
 * pathname. The pathname may be absolute (relative to the root directory
 * of the file system) or relative to the current directory in which the program
 * is running.

即一个使用路径名来指定的文件系统实体的“抽象”代表。(楼主的理解是就是一个资源的抽象,代表一个指定路径下的资源)

这个路径名可能是一个绝对的(相对于文件系统的根目录)或者是相对的对于在这个运行程序的当前目录。

File.separator windows下是“\” unix下是“/”,考虑跨平台性可以写成这样“E:” +File.separator + "test" +File.separator+"········"

file.mkdir();//只能创建一级文件夹。
file.mkdirs();//功能 会创建多级文件夹。

输入输出流:


/**
	 * 输入输出流(I/O)操作,路径可能是绝对路径也可能是相对路径
	 * @throws IOException 
	 */
	private void createfile() throws IOException{
		File file = new File("E:\\test");
		if (!file.exists()) {//文件夹不存在
			file.mkdir();//只能创建一级文件夹。
//			file.mkdirs();//功能 会创建多级文件夹
		}
		
		File file2 = new File(file.getAbsoluteFile()+"\\test.txt");
		file2.createNewFile();//创建一个新的文件
	}
	/**
	 * 不考虑有过滤器的情况,列出此目录下的所有子目录即文件名称
	 */
	private void showallfile(){
		System.out.println(File.separator);//跨平台
		String stringpath = "E:"+File.separator+"test"+File.separator;
		File file2 =  new File(stringpath);
		String[] name = file2.list();
		String nameString = "";
		for (int i = 0; i < name.length; i++) {
			nameString = nameString + name[i] + "\n";
			area.setText(nameString);
		}
	}
	/**
	 * 考虑有过滤器的情况
	 */
	private void showallfilefilter(){
		String stringPath = "E:" +File.separator + "test";
		File files = new File(stringPath);
		if (!files.exists()) {
			files.mkdirs();
		}
		
		String[] names = files.list(new FilenameFilter() {//在监听方法里写对文件进行过滤的逻辑
			
			@Override
			public boolean accept(File dir, String name) {
				System.out.println("dir:" + dir + " name:" + name);
				name = name.substring(name.length() -4, name.length());
				return name.indexOf(".txt") != -1;//过滤条件,只返回txt文本文件
			}
		});
		String name = "";
		for (int i = 0; i < names.length; i++) {
			name = name + names[i] + "\n";
		}
		area.setText(name);
	}
	
	/**
	 * 下面是文件的写操作,以下方法只是一种方式,还可以用FileWrite或BufferedWriter来写入
	 * @throws IOException 
	 */
	String writeParams = "\n"+"哈哈哈哈,成功写入"+"\n";
	private void writeFile() throws IOException{
		String stringPath = "E:" +File.separator + "test"+File.separator + "text.txt";
		File file = new File(stringPath);
		//定义一个输出流,从程序向文件输出内容
//		FileOutputStream outputStream = new FileOutputStream(file);
		FileOutputStream outputStream = new FileOutputStream(file, true);//追加
		byte[] writeByte =  writeParams.getBytes();
	    outputStream.write(writeByte);//将字节数组以流的方式写入文件
	    outputStream.flush();
	    outputStream.close();
	}
	/**
	 * 下面是文件的读操作,还可以用FileReader或BufferedReader来读出数据
	 * @throws IOException 
	 */
	private void readFile() throws IOException{
		String stringPath = "E:" +File.separator + "test"+File.separator + "text.txt";
		File file = new File(stringPath);
		//定义一个输入流,从文件向程序里进内容
		FileInputStream inputStream = new FileInputStream(file);
		byte[] bs = new byte[1024];
		while (inputStream.read(bs) != -1) {//将数据以流的方式读入到字节数组中
			String string = new String(bs);
//android中需要这样处理,因为编码格式和电脑不一样而出现中文乱码
//            String fileContent = new String(bs,"utf-8");
			area.setText(string);
		}
		inputStream.close();
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值