编写和读取字符串文件

时间:2014-01-24 09:12:48

标签: java

嗨朋友这是我的程序来创建(写)一个整数文件数组并读取它...它正在工作..现在我想创建一个Strings文件。有人可以帮我吗?

public class File {

    public static void main(String args[]) throws IOException{

        int[] array={010100101,0010010};
        OutputStream os=new FileOutputStream("arun");
        for(int i=0;i<array.length;i++){
            os.write(array[i]);
        }
        InputStream is=new FileInputStream("arun");
        for(int i=0;i<array.length;i++){
            System.out.println(is.read());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需使用String的ByteArray。

String[] array = { "010100101", "0010010" };
try {
    // write
    OutputStream os = new FileOutputStream("arun");
    for (int i = 0; i < array.length; i++) {
        os.write(array[i].getBytes(Charset.forName("UTF-8")));
    }
    // read
    InputStream is = new FileInputStream("arun");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line;
    while (( line = reader.readLine() ) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    System.err.println("You fucked up!");
}