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

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

问题描述

我已经有一个二维字符串数组存储大量数据的简单程序。我搜索了很多有关如何存储和检索二维数组的地方。我想保存在我的数组中的数据在节目的结束,并具有该数据经节目的开始检索。我努力了:   ObjectOutputStream的TOFILE =新的ObjectOutputStream(新的FileWriter(array.DATA));             toFile.writeObject(阵列); 这想出了错误:不兼容的类型: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.

在预先感谢

推荐答案

在这个例子:   这个例子很简单(它不是一个完美的解决方案,但它是一个良好的开端undestand如何保存和检索的作品)。

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 [] [] =新的INT [12] [12];

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

//或者是一个字符串[] [] =新的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();
   }

} //方法结束

}//end of method

使用一个简单的PrintWriter您保存数组到一个文件exaclty是怎么回事只要你知道改变我已经把该文件的路径;

  • 第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

使用扫描仪类检索您的阵列从已保存的文件恢复。

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

Also a better solution but you have to dig more here

让我知道你的任务完成了。

Let me know it your job is done..

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

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