java i/o流

//字節流的讀 public class read_zifu {java

public static void main(String args[]) {
	test();
}
public static void test() {
	String path = "D:/testjava/test.txt";
	InputStream is = null;
	try {
		is = new FileInputStream(path);
		byte[] bs = new byte[1024];
		int len = 0;
		while ((len = is.read(bs)) != -1) {
			System.out.println(len);
	
			System.out.println(new String(bs, 0, len));
		}
		is.close();
	} catch (Exception e) {
		// TODO: handle exception
	}

}

} //字節流的寫操做 public class write_zijie {code

public static void main(String args[]) throws Exception {
	test();
}

public static void test() throws Exception { String path="D:/testjava/1.txt"; OutputStream is=null; String str="我來自哪裏"; is =new FileOutputStream(path); is.write(str.getBytes()); is.close(); } }get

//字符流的讀操做 public class read_fileReader {it

public static void main(String args[]) throws IOException {
	test();
}

public static void test() throws IOException { Reader fi=null; String path="D:/testjava/1.txt"; try { fi=new FileReader(path); //存儲讀取的數據 char ch []=new char[1024]; //存儲該文件的大小,有多少個字符 int len=0; while((len=fi.read(ch))!=-1) { System.out.println(len); System.out.println(new String(ch,0,len));io

}
} catch (FileNotFoundException e) {
	
	// TODO Auto-generated catch block
	e.printStackTrace();
}

} }class

//字符流的寫操做 public class write_FileReader { public static void main(String args[]) throws IOException { test(); } public static void test() throws IOException { String path="D:/testjava/1.txt"; Writer w=null; //true表示容許追加 w=new FileWriter(path,true); String str="咱們都同樣"; int len=0; w.write(str); w.close();test

} }file

相關文章
相關標籤/搜索