Java文件读写操作

文件读写

InputSteam和OutputSream是以字节为单位的,Reader,Writer都是以字符为单位的操作,可以有效避免中文乱码。

文件按字节或者按字符输入

文件读

FileInputStream readFile = null;
//FileReader readFile = null;
	try {
		readFile = new FileInputStream("FileInputStreamDemo.java");
        //readFile = new FileReader("FileInputStreamDemo.java");
		int intTemp = readFile.read();
		while(intTemp!=-1){
			System.out.print((char)intTemp);
			intTemp = readFile.read();
		}
	} catch(IOException e) {
		e.printStackTrace();
	} finally {
		if(readFile != null) {
			try {
				readFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

文件写

FileOutputStream writeFile = null;
// FileWriter writeFile = null;
try {
	writeFile = new FileOutputStream("output.txt");
	// writeFile = new FileWriter("output.txt");
	String s = "happy new year!happy everyday!" + NEW_LINE
			+ "wish fulfillment!" + NEW_LINE 
			+ "你好" + NEW_LINE
			+ "End of file stream." + NEW_LINE;

	for (int i = 0; i < s.length(); i++) {
		writeFile.write(s.charAt(i));
	}

	// byte[] bytes = s.getBytes();
	// writeFile.write(bytes);
	// writeFile.write(bytes,0,s.length());

} catch (IOException e) {
	e.printStackTrace();
} finally {
	if (writeFile != null) {
		try {
			writeFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
  }
}

分多次读入文件

FileInputStream readFile = null;
try {
	readFile = new FileInputStream("FileInputStreamDemo.java");
	byte[] buffer = new byte[1024]; //设置一次读入的字节数
	int byteLength = readFile.read(buffer, 0, 1024); //读取文件到buffer中并返回读取的字节数
	StringBuilder sb = new StringBuilder(); //当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
	while (byteLength != -1) {
		sb.append(new String(buffer, 0, byteLength)); //将buffer内容添加到StringBuilder中
		byteLength = readFile.read(buffer, 0, 1024);
	}
	System.out.println(sb.toString());
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		readFile.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

文件按行读写

文件读

//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Syatem.in); 从控制台输入
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
    String line = bufferedReader.readLine();
    while (line != null) {
        line = bufferedReader.readLine();
    }
} catch (IOException e) {
    System.out.println(e.getMessage());
}

文件写

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/" + fileName))) {
      ArrayList<String> sites = new ArrayList<String>();
      Iterator<String> it = sites.iterator();
      while (it.hasNext()) {
          bufferedWriter.write(it.next());
          bufferedWriter.write("\n");
      }
  } catch (IOException e) {
      e.printStackTrace();
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值