【问题标题】:Store 2D array (String) to file and retrieve it将二维数组(字符串)存储到文件并检索它
【发布时间】:2015-04-15 11:42:15
【问题描述】:

我制作了一个简单的程序,其中包含一个存储大量数据的二维字符串数组。我搜索了很多地方来了解如何存储和检索二维数组。我想在程序结束时将数据保存在我的数组中,并在程序启动时检索这些数据。我努力了: ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA")); toFile.writeObject(array);这个出现了错误:不兼容的类型:FileWriter 无法转换为OutputStream

我只知道基本的编程,所以尽量对我轻松一点,并彻底解释你所说的一切。老实说,我什至不知道“ObjectOutputStream”是什么。

在此先感谢

【问题讨论】:

  • 您需要稍等片刻,我将向您展示完整的代码示例!
  • @crAlex 非常感谢!

标签: java arrays object syntax 2d


【解决方案1】:

您应该改用 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();
    }
}

【讨论】:

  • 感谢@RockSolid 的回复你能告诉我你提供的代码的每一部分是做什么的吗?
  • 我还需要知道如何检索文件
  • 尝试简单的复制粘贴程序并运行(程序 args 1 2 3 4 5)用于读取使用 ObjectInputStream.FileInputStream 和 readObject
【解决方案2】:

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

您的二维数组是 int a[][]= 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

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

【讨论】:

  • 谢谢。你如何将信息检索到数组中?这对字符串有用吗? e.printStackTrace(); 是什么?做? @crAlex
  • 是的,它适用于字符串。我会改造它。 e.printStackTrace() 如果发现问题,只需在控制台中打印问题。
  • 粘贴方法并编译后出现错误找不到符号。我为我的数组变量得到这个
  • 你的数组使用E String 还是int?
  • 我已经解决了这个错误。我现在需要的是如何在程序开始时将文件数据检索到数组中
【解决方案3】:

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

例如

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多