输入和输出

Java代码 
1.    // Stream, Reader, Writer  
2.    Stream: 处 理字节流  
3.    Reader/Writer: 处理字符,通用Unicode  
4.      
5.    // 从标准输入设备读数据  
6.    1. 用System.in的BufferedInputStream()读取字节  
7.        int b = System.in.read();  
8.          System.out.println("Read data: " + (char)b);  // 强 制转换为字符  
9.    2. BufferedReader 读取文本  
10.        如果从Stream转成Reader,使用 InputStreamReader类  
11.        BufferedReader is = new BufferedReader(new   
12.          InputStreamReader(System.in));  
13.          String inputLine;  
14.          while ((inputLine = is.readLine()) != null) {  
15.              System.out.println(inputLine);  
16.              int val = Integer.parseInt(inputLine);  // 如果inputLine为整数  
17.        }  
18.          is.close();  
19.          
20.    // 向标准输出设备写数据  
21.    1. 用System.out的println()打印数据  
22.    2. 用PrintWriter打印  
23.        PrintWriter pw = new PrintWriter(System.out);  
24.          pw.println("The answer is " + myAnswer + " at this time.");  
25.          
26.    // Formatter类  
27.    格 式化打印内容  
28.    Formatter fmtr = new Formatter();  
29.    fmtr.format("%1$04d - the year of %2$f", 1951, Math.PI);  
30.    或 者System.out.printf();或者System.out.format();        
31.      
32.    // 原始扫描  
33.    void doFile(Reader is) {  
34.        int c;  
35.        while ((c = is.read()) != -1) {  
36.            System.out.println((char)c);  
37.        }  
38.    }    
39.      
40.    // Scanner扫描  
41.    Scanner 可以读取File, InputStream, String, Readable  
42.    try {  
43.        Scanner scan = new Scanner(new File("a.txt"));  
44.        while (scan.hasNext()) {  
45.            String s = scan.next();  
46.        }  
47.        } catch (FileNotFoundException e) {  
48.            e.printStackTrace();  
49.        }  
50.    }  
51.      
52.    // 读取文件  
53.    BufferedReader is = new BufferedReader(new FileReader("myFile.txt"));  
54.    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bytes.bat"));  
55.    is.close();  
56.    bos.close();  
57.      
58.    // 复制文件  
59.    BufferedIutputStream is = new BufferedIutputStream(new FileIutputStream("oldFile.txt"));  
60.    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("newFile.txt"));  
61.    int b;  
62.    while ((b = is.read()) != -1) {  
63.        os.write(b);  
64.    }  
65.    is.close();  
66.    os.close();  
67.      
68.    // 文件读入字符串  
69.    StringBuffer sb = new StringBuffer();  
70.    char[] b = new char[8192];  
71.    int n;  
72.    // 读一个块,如果有字符,加入缓冲区  
73.    while ((n = is.read(b)) > 0) {  
74.        sb.append(b, 0, n);  
75.    }  
76.    return sb.toString();  
77.      
78.    // 重定向标准流  
79.    String logfile = "error.log";  
80.    System.setErr(new PrintStream(new FileOutputStream(logfile)));  
81.      
82.    // 读写不同字符集文本  
83.    BufferedReader chinese = new BufferedReader(new InputStreamReader(new FileInputStream("chinese.txt"), "ISO8859_1"));  
84.    PrintWriter standard = new PrintWriter(new OutputStreamWriter(new FileOutputStream("standard.txt"), "UTF-8"));  
85.      
86.    // 读取二进制数据  
87.    DataOutputStream os = new DataOutputStream(new FileOutputStream("a.txt"));  
88.    os.writeInt(i);  
89.    os.writeDouble(d);  
90.    os.close();  
91.      
92.    // 从指定位置读数据  
93.    RandomAccessFile raf = new RandomAccessFile(fileName, "r");  // r表示已 只读打开  
94.    raf.seek(15);  // 从15开始读  
95.    raf.readInt();  
96.    raf.radLine();  
97.      
98.    // 串行化对象  
99.    对象串 行化,必须实现Serializable接口  
100.    // 保存 数据到磁盘  
101.    ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(FILENAME)));  
102.    os.writeObject(serialObject);  
103.    os.close();  
104.    // 读出数据  
105.    ObjectInputStream is = new ObjectInputStream(new FileInputStream(FILENAME));  
106.    is.readObject();  
107.    is.close();  
108.      
109.    // 读写Jar或Zip文档  
110.    ZipFile zippy = new ZipFile("a.jar");  
111.    Enumeration all = zippy.entries();  // 枚举值列出所有文件清单  
112.    while (all.hasMoreElements()) {  
113.        ZipEntry entry = (ZipEntry)all.nextElement();  
114.        if (entry.isFile())  
115.            println("Directory: " + entry.getName());  
116.              
117.        // 读写文件  
118.        FileOutputStream os = new FileOutputStream(entry.getName());  
119.        InputStream is = zippy.getInputStream(entry);  
120.        int n = 0;  
121.        byte[] b = new byte[8092];  
122.        while ((n = is.read(b)) > 0) {  
123.            os.write(b, 0, n);  
124.            is.close();  
125.            os.close();  
126.        }  
127.    }  
128.      
129.    // 读写gzip文档  
130.    FileInputStream fin = new FileInputStream(FILENAME);  
131.    GZIPInputStream gzis = new GZIPInputStream(fin);  
132.    InputStreamReader xover = new InputStreamReader(gzis);  
133.    BufferedReader is = new BufferedReader(xover);  
134.    String line;  
135.    while ((line = is.readLine()) != null)  
136.        System.out.println("Read: " + line);  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值