输入流:
public void inputDemo () throws IOException {
//文件名称 String fileName = "d:\aaa.txt";
//通过文件名称创建文件对象 File file = new File(fileName);
//创建一个输入流对象
// new FileOutputStream(file); FileInputStream fis = new FileInputStream(fileName); // int c; // while ((c = fis.read()) > 0) { // System.out.print((char)c); // } //创建一个字节数组 byte[] buf = new byte[2000]; int len;
//没有找到内容返回-1,所以判断有没内容。有则输出 while ((len = fis.read(buf)) > 0) { System.out.println("新的一杯:"); System.out.println(new String(buf)); } //关闭输入流 fis.close(); }
输出流:
public void outputDemo () throws IOException {
//创建输出对象流 FileOutputStream fos = new FileOutputStream("d:\bbb.txt");
//通过输出创建写入流 OutputStreamWriter writer = new OutputStreamWriter(fos);
//通过写入流写入内容 writer.write("hello, output stream"); writer.close(); }