码迷,mamicode.com
首页 > 其他好文 > 详细

FileReader类和FileWriter类的基本用法示例

时间:2016-02-21 12:43:43      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.io;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class NewClass4 {
    public static void main(String[] args) {
        char a[] = "今晚10点发动总攻".toCharArray();
        int n;
                
        try {
            File file = new File("d:\\java", "aa.java");
            for (int i = 0; i < a.length; i++) {
                a[i] = (char) (a[i] ^ ‘R‘);
            }
            FileWriter out = new FileWriter(file);
            out.write(a, 0, a.length);
            out.close();
            
            FileReader in = new FileReader(file);
            char[] buf = new char[10];
            System.out.println("密文:");
            while ((n = in.read(buf, 0, 10))!= -1) {              
                // n 为实际读取的字符数
                String s = new String(buf, 0, n);
                System.out.println(s);
            }
            in.close();
            
            in = new FileReader(file);
            System.out.println("明文:");
            while((n = in.read(buf, 0, 10))!= -1) {
                for (int i = 0; i < n; i++) {
                    buf[i] = (char) (buf[i] ^ ‘R‘);
                }
                String str = new String(buf, 0, n);
                System.out.println(str);
            }
            in.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

 

FileReader类和FileWriter类的基本用法示例

标签:

原文地址:http://www.cnblogs.com/qixin622/p/5204607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!