简体   繁体   English

如何在Java中附加文件

[英]how to append file in java

How do I append the highscore into highscore.txt file at the correct position in descending order? 如何将highscore按降序附加到highscore.txt文件中的正确位置? The method uses readHighScore(int score) method created previously to find the line number where the record should be inserted. 该方法使用之前创建的readHighScore(int score)方法来查找应在其中插入记录的行号。 This is what I got so far: 这是我到目前为止所得到的:

public static void recordHighScore(String name, int score){
            File file = new File("Test/highscore.txt");
            FileWriter fwriter = new FileWriter(file, true);
            try{
                String userName = name+","+score;
                fwriter.add(readHighScore(score), userName);

            }catch(IOException io){
                System.out.println("Missing File!");

            }

            /*Writes the high score into highscore.txt file, at the correct position (in descending order). This
            method uses readHighScore( ) method to find the line number where the record should be
            inserted.*/
        }

It's very easy if you want to append data at the end, and almost impossible if you want append data at the middle. 如果要在末尾附加数据非常容易,而如果要在中间附加数据几乎是不可能的。 Much easy read all file in memory, change it and save all file again, overwriting old informaton. 轻松读取内存中的所有文件,进行更改并再次保存所有文件,覆盖旧的信息非常容易。

For example: 例如:

// read file to arraylist
Scanner s = new Scanner(new File("filepath"));
List<Score> list = new ArrayList<Score>();
while (s.hasNext()){
    String[] a = s.next().split(","); 
    list.add(new Score(a[0], a[1]); ); // Score is class with name and score fields
}
s.close();

// working with List<Score>
// add new score to List<Score>
// save List<Score> to file like in your code

如何 append arraylist<object> 在 java 的文件中<div id="text_translate"><p>给定的 function 写入文件但再次调用它会覆盖文件中的先前列表 object 例如</p><p>如果数组列表索引 0 包含名称 - “aman”,我称之为 function 它将“aman”保存在文件中。 但是当索引 1 的名称为“lavesh”时,它会覆盖以前的数据</p><p>它在像“lavesh”这样的文件中,但我想要它像“aman lavesh”</p><pre class="lang-java prettyprint-override"> public void alter_file( ArrayList&lt;Flight_registrie&gt; x) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(" Flight Registrie.txt")); oos.writeObject(x); x.clear(); oos.close(); } catch (IOException e) { System.out.println("An I/O error occurs"); e.printStackTrace(); } }</pre><p> 没有错误,但我的方法可能是错误的</p></div></object> - How to append arraylist<object > in a file in java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java文件中附加数据? - how to append the data in java file? 如何在Java中附加XML文件 - How to append xml file in java 如何将 append 到 java 中的文件末尾? - How to append to the end of a file in java? 如何在Java上的文件上附加对象? - How to append objects on a file on Java? 如何在Java中的文件上附加对象? - How to append objects on a file in Java? 如何在Java中将数据插入文件? 没有附加 - How to insert the data into the file in java? No Append Java - 如何将某些内容附加到文件中的特定行 - Java - How to append something to a specific line in a file 如何在java中的文件中循环写入或附加字符串? - How to write or append string in loop in a file in java? 如何 append arraylist<object> 在 java 的文件中<div id="text_translate"><p>给定的 function 写入文件但再次调用它会覆盖文件中的先前列表 object 例如</p><p>如果数组列表索引 0 包含名称 - “aman”,我称之为 function 它将“aman”保存在文件中。 但是当索引 1 的名称为“lavesh”时,它会覆盖以前的数据</p><p>它在像“lavesh”这样的文件中,但我想要它像“aman lavesh”</p><pre class="lang-java prettyprint-override"> public void alter_file( ArrayList&lt;Flight_registrie&gt; x) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(" Flight Registrie.txt")); oos.writeObject(x); x.clear(); oos.close(); } catch (IOException e) { System.out.println("An I/O error occurs"); e.printStackTrace(); } }</pre><p> 没有错误,但我的方法可能是错误的</p></div></object> - How to append arraylist<object > in a file in java 如何使用格式化程序附加到java中的文件? - How to append to a file in java using formatter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM