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

相关文章
相关标签/搜索