1

我在将数组获取到字符串中的文本文件时遇到问题。到目前为止我的代码:

public class ArrayList 
{

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

        int myArray [] = new int [20]; 

        for (int i = 0 ; i < 20 ; i++) { 
                        myArray [i] = (int) (Math.random () * 8);
            System.out.print(myArray[i]);
            String s1 = Arrays.toString(myArray);
            System.out.println(s1);

            s1 = "/Users/EricDkim/Desktop/FileIOTest/pfile.txt";
            File f = new File(s1);
            FileOutputStream fileOut = new FileOutputStream(f);

            byte value = 0x63; //By adding 0x it reads it as hex
            DataOutputStream dataOut = new DataOutputStream(fileOut);
            //dataoutputstream is used to write primitive java
            //data types (byte, int, char, double) and strings are to a file or a socket
            dataOut.writeByte(value);

            //creates 20 numbers
        }

    }

}

我将如何使用我创建的数组将其移动到文本文件中?

4

2 回答 2

1

如何使用DataOutputStream#writeInt(int)

for (int i = 0 ; i < myArray.length ; i++) { 
    dataOut.writeInt(myArray[i]);
}

如果你想写成文本,那么使用BufferedWriter

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOut));
for (int i = 0 ; i < myArray.length ; i++) {
    bw.write(Integer.toString(myArray[i]));
}
bw.close();

不要忘记关闭流/编写器。

尝试

File f = new File(s1);
FileOutputStream fileOut = new FileOutputStream(f);
int myArray [] = new int [20];
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOut));
for (int i = 0 ; i < myArray.length ; i++) {
    myArray [i] = (int) (Math.random () * 8);
    bw.write(Integer.toString(myArray[i]));
    bw.write(',');
}
bw.close();

该文件的内容将是

0,5,1,3,4,0,0,3,0,6,7,6,4,1,1,6,0,6,7,4,

或者

File f = new File(s1);
FileOutputStream fileOut = new FileOutputStream(f);
int myArray [] = new int [20];
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOut));
for (int i = 0 ; i < myArray.length ; i++) {
    myArray [i] = (int) (Math.random () * 8);
}
bw.write(Arrays.toString(myArray));
bw.close();

该文件的内容将是

[3, 3, 4, 4, 6, 1, 0, 2, 3, 5, 7, 7, 0, 5, 1, 2, 0, 4, 6, 4]
于 2013-11-12T17:50:12.313 回答
0

Declare

 File f = new File(s1);
 FileOutputStream fileOut = new FileOutputStream(f);
 DataOutputStream dataOut = new DataOutputStream(fileOut);

before the for loop and close tour stream at the end of loop like this dataOut.close();

For writing the contents as text check this edited code.

public class ArrayList {

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

    int myArray[] = new int[20];
    String s1 = "/Users/EricDkim/Desktop/FileIOTest/pfile.txt";
    File f = new File(s1);
    Writer fileOut = new FileWriter(f);
    BufferedWriter dataOut = new BufferedWriter(fileOut);
    for (int i = 0; i < 20; i++) {
        myArray[i] = (int) (Math.random() * 8);
        // System.out.print(myArray[i]);
        // String s1 = Arrays.toString(myArray);
        System.out.println(s1);
        byte value = 0x63; // By adding 0x it reads it as hex
        // dataoutputstream is used to write primitive java
        // data types (byte, int, char, double) and strings are to a file or
        // a socket
        fileOut.write(Arrays.toString(myArray) + "\n");
        // creates 20 numbers
    }
    dataOut.close();

}

}
于 2013-11-12T17:54:57.057 回答