簡體   English   中英

將2D數組(字符串)存儲到文件並檢索

[英]Store 2D array (String) to file and retrieve it

我制作了一個簡單的程序,該程序具有一個存儲大量數據的2D字符串數組。 我在很多地方搜索了如何存儲和檢索2D數組。 我想將數據保存在程序末尾的數組中,並在程序開始時獲取此數據。 我已經嘗試過: ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA")); toFile.writeObject(array); ObjectOutputStream toFile = new ObjectOutputStream(new FileWriter("array.DATA")); toFile.writeObject(array); 出現錯誤:不兼容的類型:FileWriter無法轉換為OutputStream

我只了解基本編程,所以請盡量輕松一點,並全面解釋您說的所有內容。 老實說,我什至不知道“ ObjectOutputStream”是什么。

提前謝謝了

在這個例子中:這個例子非常簡單( 這不是一個完美的解決方案,但是卻是理解保存和檢索工作原理的一個良好的開端 )。

您的2d數組是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類,可以從保存的文件中檢索陣列。

也是更好的解決方案,但您必須在這里進行更多挖掘

讓我知道您的工作已經完成。

還有一個要解決的問題,即如何存儲數組的大小和內容。 我建議使用轉換為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

您應該改為使用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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM