首页 > 代码库 > io流之inputstream、outputstream、reader、writer
io流之inputstream、outputstream、reader、writer
例子程序:读取工作空间下
package io;import java.io.*;public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("F:\\share\\workspace\\J2SE\\src\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("找不到文件"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("共读取了 "+num+" 个字节"); } catch (IOException e1) { System.out.println("文件读取错误"); System.exit(-1); } }}
结果:由于read方法一次读取的是一个字节,一个汉字占的2个字节,所以打印出来汉字是乱码,
package io;import java.io.*;public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("F:\\share\\workspace\\J2SE\\src\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("æ?¾ä¸?å?°æ??件"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("å?±è¯»å??äº? "+num+" 个å?è??"); } catch (IOException e1) { System.out.println("æ??件读å??é??误"); System.exit(-1); } }}共读取了 737 个字节
OutputStream:
例子程序:文件的复制,如果文件不存在会自动创建HW.java 文件
package io;import java.io.*;public class TestFileOutputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("f:/share/j2se/IO/HelloWorld.java"); out = new FileOutputStream("f:/share/j2se/IO/HW.java"); while((b=in.read())!=-1){ out.write(b); } in.close(); out.close(); } catch (FileNotFoundException e2) { System.out.println("找不到指定文件"); System.exit(-1); } catch (IOException e1) { System.out.println("文件复制错误"); System.exit(-1); } System.out.println("文件已复制"); }}
Reader:
例子程序:读取本身
package io;import java.io.*;public class TestFileReader { public static void main(String[] args) { FileReader fr = null; int c = 0; try { fr = new FileReader("F:\\share\\workspace\\J2SE\\src\\io\\TestFileReader.java"); int ln = 0; while ((c = fr.read()) != -1) { //char ch = (char) fr.read(); System.out.print((char)c); //if (++ln >= 100) { System.out.println(); ln = 0;} } fr.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); } catch (IOException e) { System.out.println("文件读取错误"); } }}
Writer:
例子程序“
package io;import java.io.*;public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("f:\\share\\j2se\\IO\\unicode.dat"); //char在计算机是用数字表示的,直接把数字写进文件。char是2个字节,最大是65535 //这里就是吧Unicode大多数国家的字符都写进了文件 for(int c=0;c<=50000;c++){ fw.write(c); if(c%100==0){ fw.write("\n"); } } fw.close(); } catch (IOException e1) { e1.printStackTrace(); System.out.println("文件写入错误"); System.exit(-1); } }}
结果:
!"#$%&‘()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~???????????????????????????????? ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿāā????????????????ēē??????ěě??????????????īī????????????????????????ń???ň???ōō????Œœ????????????Šš????????ūū????????????Ÿ?????????????????????????ƒ??????????????????????????????????????????????∥???????????ǎǎǐǐǒǒǔǔǖǖǘǘǚǚǜǜ???
????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????
io流之inputstream、outputstream、reader、writer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。