将二维数组(字符串)存储到文件并检索它 [英] Store 2D array (String) to file and retrieve it

查看:27
本文介绍了将二维数组(字符串)存储到文件并检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的程序,它有一个存储大量数据的二维字符串数组.我已经搜索了很多关于如何存储和检索二维数组的地方.我想在程序结束时将数据保存在我的数组中,并在程序开始时检索这些数据.我试过了:ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA"));toFile.writeObject(array); 这个就出现了错误:不兼容的类型:FileWriter 不能被转换为OutputStream

I have made a simple program that has a 2D String array storing lots of data. I have searched a lot of places for how to store and retrieve 2D arrays. I want to save the data in my array at the end of the program and have this data retrieved upon the start of the program. I have tried: ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA")); toFile.writeObject(array); This came up with the error: incompatible types: FileWriter cannot be converted to OutputStream

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

I only know basic programming so try to be easy on me and explain everything you say thoroughly. If I'm honest I don't even know what 'ObjectOutputStream' is.

非常感谢

推荐答案

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

In this example: This example is very simple(it's not a perfect solution but it's a good start to undestand how save and retrieve works).

你的二维数组是 int a[][]= new int[12][12];

Your 2d array is int a[][]= new int[12][12];

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

//or a is String a[][] = new String[12][12];

  • 第 1 部分:(保存)

  • Part 1: (Save)

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 部分:(打开时更新或检索)

  • Part 2: (Update or Retrieve on Open)

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 类,您可以从保存的文件中检索数组.

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

Also a better solution but you have to dig more here

告诉我你的工作已经完成..

Let me know it your job is done..

这篇关于将二维数组(字符串)存储到文件并检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆