Java.io.字符流

//字符流,Reader和Writer
	
	//字符输出流Writer
	//向文件写入一个字符串
	public static void main(String[] args) throws Exception {
		String path="d:"+File.separator+"T2.txt";//找到文件路径
		File file = new File(path);

//		Writer fileWriter = new FileWriter(file);
		Writer fileWriter = new FileWriter(file,true);//true,后面追加字符串,不覆盖之前的字符串
		
		//直接输出字符串,不用先转为byte数组
		fileWriter.write("\r\nsuccess");
		fileWriter.close();
	}



// 字符输入流Reader
	public static void main(String[] args) throws Exception {
		String path = "d:" + File.separator + "T2.txt";//找到文件路径
		File file = new File(path);

		Reader reader = new FileReader(file);
		
		char[] chars = new char[1000];//读取的数据,保存在这个数组
		int read_len = reader.read(chars);////读取到的文件大小
		
		reader.close();
		System.out.println(new String(chars, 0, read_len));
	}

猜你喜欢

转载自8850702.iteye.com/blog/2281734