import java.io.FileReader; public class TestFile { public static void main(String[] args)throws Exception{ FileReader f = new FileReader("D:\\b站马士兵java基础\\线程\\src\\com\\bzmsbxc\\Test1.java"); int d; d = f.read(); while(-1!=d){ System.out.printf("%c",(char)d); d = f.read(); } f.close(); } }
运行结果
字符流
import java.io.FileReader; import java.io.FileWriter; public class TestFile { public static void main(String[] args)throws Exception{ //字符流的输入 FileReader f = new FileReader("D:\\b站马士兵java基础\\线程\\src\\com\\bzmsbxc\\Test1.java"); //字符流的输出 FileWriter ff = new FileWriter("D:\\西电\\计算机\\java\\x.txt"); int d; d = f.read();//从f读写到d中
while(-1!=d){ ff.write(d);//把d写入到ff d = f.read(); } ff.flush();//刷新缓冲区 f.close(); ff.close(); } }
字节流
import java.io.*; public class TestFile { public static void main(String[] args)throws Exception{ //字节流的输入 FileInputStream f = new FileInputStream("D:\\b站马士兵java基础\\线程\\src\\com\\bzmsbxc\\Test1.java"); //字节流的输出 FileOutputStream ff = new FileOutputStream("D:\\西电\\计算机\\java\\x.txt"); int d; d = f.read(); while(-1!=d){ ff.write(d); d = f.read(); } ff.flush();//刷新缓冲区 f.close(); ff.close(); } }
import java.io.*; public class TestFile { public static void main(String[] args){ try { //加缓冲区的字符流输入区(超级数据输入管道)效率更快 BufferedReader f = new BufferedReader(new FileReader("D:\\b站马士兵java基础\\线程\\src\\com\\bzmsbxc\\Test1.java")); //加缓冲区的字符流输出区(超级数据输出管道)效率更快 BufferedWriter ff = new BufferedWriter(new FileWriter("D:\\西电\\计算机\\java\\x.txt")); String str = null; while (null != (str = f.readLine())) {//读取一行字符,但会将读取的换行符自动丢弃,即返回的String对象中并不包括换行符 ff.write(str); str = f.readLine(); ff.newLine();//写入下一行 } ff.flush(); f.close(); ff.close(); }catch(Exception e){ e.printStackTrace(); System.exit(-1); } } }
import java.io.*; /** * 把长整型数据放入字节数组,并读取 */ public class TestData { public static void main(String[] args)throws Exception{ //写入 ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream d = new DataOutputStream(b); long m = 2345522; d.writeLong(m); byte[] by = b.toByteArray();//DataOutputStream 流中并没有toByteArray()方法,但是ByteArrayOutputStream 流中却有toByteArray()方法, //所以不可以把b 改为d,否则编译时会出错! ByteArrayOutputStream流中toByteArray()方法的含义, //摘自API“创建一个新分配的 byte 数组。其大小是此输出流的当前大小,并且缓冲区的有效内容已复制到该数组中” //读取 ByteArrayInputStream bb = new ByteArrayInputStream(by); DataInputStream dd = new DataInputStream(bb); Long l = dd.readLong(); d.flush(); d.close(); dd.close(); System.out.println(l); } }
方法一; import java.io.*; /** * 通过键盘向一个文件中写入数据,即把用户从键盘敲入的数据自动写入某个文件中, * 如果敲入的是exit,则程序终止 */ public class TestPrintf { public static void main(String[] args) { String s = null; PrintStream p = null; BufferedReader b = null; try { b = new BufferedReader(new InputStreamReader(System.in)); p = new PrintStream(new FileOutputStream("E:\\尚学堂\\第二阶段视频\\Printf\\java\\printf.txt")); while (null != (s = b.readLine())) { if (s.equalsIgnoreCase("exit")) { break; } p.printf(s); } ; p.flush(); } catch (Exception e) { System.exit(-1); } finally { try { b.close(); } catch (IOException e) { e.printStackTrace(); } p.close(); } } }
方法二: import java.io.*; /** * 通过键盘向一个文件中写入数据,即把用户从键盘敲入的数据自动写入某个文件中, * 如果敲入的是exit,则程序终止 */ public class TestPrintWriter { public static void main(String[] args) { BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); PrintWriter ps = null; try { ps = new PrintWriter(new FileWriter("d:/share/java/BB.java")); String strRec = null; while (null != (strRec=br.readLine())) //记住: 如果直接输入回车的话, br.readLine() 返回的是"" 而不是 null 或者讲:br.readLine()遇到回车符时终止读取,并且会把读取到的回车符自动丢弃掉 { if (strRec.equalsIgnoreCase("exit")) break; ps.println(strRec); } ps.flush(); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } finally { ps.close(); } } }
/** *将原本的System.out.println()打印在公屏上,通过setOut()和printfOutstream流转换成打印字节流文件上 / public class TestSetSystemOut { public static void main(String[] args) { PrintStream ps_out = null; try { ps_out = new PrintStream(new FileOutputStream("d:/share/ww.txt")); System.setOut(ps_out); //将System.out的值重新设置为ps_out,即System.out不在关联到显示器,而是关联到"d:/share/ww.txt"文件 System.out.println(12); //这实际上是把12输出到了System.out所关联的d:/share/ww.txt中 System.out.println(55.5); //同上 } catch (Exception e) { e.printStackTrace(); } finally { try { ps_out.close(); } catch (Exception e) { e.printStackTrace(); } } } }
/* 功能: 将键盘输入的数据输入A文件中,如果输入有误, 则把出错信息输出到B文件中 标准输入输出流的重定向 */ import java.io.*; import java.util.*; public class TestSetOutErr { public static void main(String[] args) { PrintStream psOut = null; PrintStream psError = null; Scanner sc = null; try { psOut = new PrintStream("d:/Out.txt");//文件A psError = new PrintStream("d:/error.txt");//文件B sc = new Scanner(System.in); int num; System.setOut(psOut); System.setErr(psError); while (true) { num = sc.nextInt(); System.out.println(num);//不能省 } } catch (Exception e) { System.err.println("出错的信息是:"); //不可以写成System.out.println("出错的信息是:"); e.printStackTrace(); //e.printStackTrace(); 默认是输出到System.err所关联的设备中 } } }
import java.io.*; ** * 将对象写入字节流,再读取该对象(用另一个引用)然后打印该对象 */ public class TestObjectIO { public static void main(String[] args) { ObjectOutputStream oos = null; ObjectInputStream ois = null; Student ss = new Student("zhansan", 1000, 88.8f); //注意88.8f不能改为88.8 Student ss2 = null; try { FileOutputStream fos = new FileOutputStream("d:/share/java/ObjectOut.txt"); oos = new ObjectOutputStream(fos); oos.writeObject(ss); ois = new ObjectInputStream(new FileInputStream("d:/share/java/ObjectOut.txt")); ss2 = (Student)ois.readObject(); //(Student)不能省 ois.readObject();如果ois中的某个成员是transient,则该成员是不会被读取的,因为该成员不会被保存,何来读取之说?! System.out.println("ss2.sname = " + ss2.sname); System.out.println("ss2.sid = " + ss2.sid); System.out.println("ss2.sscore = " + ss2.sscore); } catch (FileNotFoundException e) { System.out.println("文件没有找到!"); System.exit(-1); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } finally { try { oos.close(); ois.close(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } } }