Java I/O操作5

1000 ワード

Bufferによるファイル操作
1.ファイルの読み込み
public static void readOneFile() throws IOException {
		//    FileReader
		Reader in = new FileReader("E://sss.txt");
		BufferedReader fin = new BufferedReader(in);

		//       
		String str1 = null;
		//not including any line-termination characters
		while ( null != (str1=fin.readLine())) {
			System.out.println(str1);
		}

		//    
		fin.close();
		in.close();
	}

2.書く
/**
	 *   BufferedWriter     
	 * 
	 * @throws IOException
	 */
	public static void showBufferedWriter() throws IOException  {
		StringWriter s = new StringWriter ();
		BufferedWriter out = new BufferedWriter(s, 5);

		// buffer  
		String str = "1234567890abcde-";
		for (char c: str.toCharArray()) {
			out.write(c);
			System.out.println(s.getBuffer());
		}

		//		out.flush();//    ,          
		//		System.out.println(s.getBuffer());

		out.close();//     ,         
		System.out.println(s.getBuffer());

		s.close();
	}