将2D数组(String)存储到文件并检索它

时间:2015-02-14 14:08:09

标签: java arrays object syntax 2d

我做了一个简单的程序,它有一个存储大量数据的2D String数组。我在很多地方搜索了如何存储和检索2D数组。我想在程序结束时将数据保存在我的数组中,并在程序启动时检索这些数据。我试过了:  ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA")); toFile.writeObject(array);这就出现了错误:不兼容的类型:FileWriter无法转换为OutputStream

我只知道基本的编程,所以尽量对我很轻松并解释你所说的一切。如果我诚实,我甚至不知道'ObjectOutputStream'是什么。

非常感谢提前

3 个答案:

答案 0 :(得分:1)

在这个例子中:   这个例子非常简单(它不是一个完美的解决方案,但它是解决保存和检索工作方式的良好开端)。

你的2d数组是int [] [] = new int [12] [12];

//或a是String a [] [] = new String [12] [12];

  • 第1部分:(保存)

    public void Save(){
    
     try {
        PrintWriter writer = new PrintWriter(new File("C:/Users/Alex.hp/Desktop/t.txt"));
    
        for(int i=0; i<a.length; i++){
            for(int j=0; j<a[i].length; j++){
    
              //use this if your array has (int,double..)
                  // writer.write(String.valueOf(a[i][j])+" "); //Here you parse the int from array to String.
    
    
               //use this if your array has String
                 writer.write(a[i][j]+" "); //Its String so you dont have to use String.valueOf(something(int,double,...)
            }
           writer.println(); //leave one line 
        }
    
        writer.flush();  //flush the writer
        writer.close();  //close the writer      
    
    
    
       } catch (FileNotFoundException e) {      
         e.printStackTrace();
       }
    

    } //方法结束

使用简单的PrintWriter,您可以将数组保存到文件中,无论其格式如何。 只是你知道改变我放到文件的路径;

  • 第2部分:(更新或打开时检索)

    public void UpdateOnOpen(){
    
     try {
        Scanner scan = new Scanner(new File("C:/Users/Alex.hp/Desktop/t.txt"));
    
    //Retrieve
            for(int i=0; i<a.length;  i++)
              for(int j=0; j<a[i].length; j++){
    
              //if you array use (int,double,...)
                //a[i][j]=Interger.parseInt(scan.next()) //for int
                //a[i][j]=Double.parseDouble(scan.next()) //for double
    
              //if your array use String
                a[i][j]=scan.next(); //Here you retrieve the array again from the file
    
    
              }//end of for(int j=0; j<a[i].length; j++)
    
    
    
         scan.close(); //close the resource file you opened
    
    //Print it to be sure all are right
              for(int i=0; i<a.length; i++){
                    for(int c=0; c<a[i].length; c++)
                          System.out.printf(""+a[i][c]+",");
    
                System.out.println();    
              }                         
    
      } catch (FileNotFoundException e){  e.printStackTrace(); }    
    
    
    }//end of method
    

    使用Scanner类,您可以从保存的文件中检索数组。

也是一个更好的解决方案,但你必须挖掘更多here

让我知道你的工作已经完成..

答案 1 :(得分:1)

还有一个问题需要解决,即如何存储数组的大小以及内容。我建议使用转换为JSON。如果您获得Jackson库,您应该能够使用Jackson Object Mapper。我建议将数组存储在另一个对象中。

e.g。

class MyClass {
    String[][] myArray;

    // add the getters and setters to this class for myArray too
}

然后存储

MyClass myObject = new MyClass();

// set the arrays here to whatever you like

ObjectMapper mapper = new ObjectMapper();
mapper.write(new File("c:\somedir\somefile.txt"), myObject);

然后加载

ObjectMapper mapper = new ObjectMapper();
MyClass loaded = mapper.readValue(new File("c:\somedir\somefile.txt", MyClass.class);

// then you can use loaded as the source of your data

答案 2 :(得分:0)

您应该使用FileOutputStream。 FileWriter的字符,而ObjectOutputStream是二进制输出流。

public static void main(String[] args) {
    try {
        ObjectOutputStream toFile = new ObjectOutputStream(new FileOutputStream("//home//user//array.DATA"));
        toFile.writeObject(args);
        toFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
相关问题