如何将代码数据写入文本文件

时间:2014-09-07 21:26:42

标签: java save text-files

我刚做了一个简单的代码,它接受用户名和电话号码,并通过创建对象将它们保存到arraylist中。我想将这些信息(名称和电话号码)保存到文本文件中,以便我可以再次获取所有旧信息。我该怎么做?这是我的代码......

import java.util.ArrayList;
import java.util.Scanner;
public class manager {

    Scanner input = new Scanner(System.in);
    ArrayList <objectclass> Test = new ArrayList <objectclass> ();


        public void mainloop() {
        for (int i = 0; i < 100; i++) {

            String x;

            System.out.println("Please Select your option");
            System.out.println("............................");
            System.out.println("1 ADD NAME AND NUMBER\n2 SEARCH NAME AND NUMBER \n0 EXIT");
            System.out.println("............................");

            x = input.nextLine();

            if (x.equalsIgnoreCase("0")) {
                System.out.println("Thank you!");
                break;
            }
            if (x.equalsIgnoreCase("1")) {
                String Name;
                String Number;

                System.out.println("Please Enter your Name below");
                Name = input.nextLine();
                System.out.println("Please Enter your Number below");
                Number = input.nextLine();

                objectclass objectclassObject = new objectclass(Name, Number);
                Test.add(objectclassObject);
            }
            if (x.equalsIgnoreCase("2")) {
                String y;
                System.out.println("*** Enter your Name below for search ***");
                y = input.nextLine();

                for (objectclass p : Test) {
                    String z = p.getName();
                    if (z.equalsIgnoreCase(y)) {
                        System.out.println("Your Name is: " + p.getName() + "\nYour Number is: " + p.getNumber());
                        System.out.println("");
                    } else {
                        System.out.println("Contact not Found!\n");
                    }
                }
            }
        }
    }
}

我想将存储在arraylist中的所有名称和编号保存到文本文件中...我该怎么办?

我到目前为止尝试了这个但是不知道下一步该做什么......     import java.io. ;     import java.lang。;     import java.util。*;

public class creatfile {

private Formatter x;

public void openFile(){
    try{
        x = new Formatter("testkyo");
    }catch (Exception e){
        System.out.println("you have an error");
    }
}
public void addRecord(){
    x.format();
}
public void closeFile(){
    x.close();
}

3 个答案:

答案 0 :(得分:0)

一个简单的例子:

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter("./output.txt"));
        writer.write("Hello World");
    } catch (IOException e) {
        System.err.println(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                System.err.println(e);
            }
        }
    }

这将在名为“output.txt”的文本文件中写入“Hello World”。

检查java I / O api。

您可以在网上找到很多有关此内容的教程,例如:

Reading, Writing, and Creating Files

Creating, Writing, Reading files using Java Files API of Java 7

答案 1 :(得分:0)

您需要序列化对象才能将其保存到文件中。

here's a tutorial on how to do it,非常简单。

序列化对象时,可以将其写入文件,然后从那里加载。

编辑:

关于如何在这里使用它的例子,我想ObjectClass是你想要保存的东西:

class ObjectClass implements Serializable {
 String name;
 String number;
 //constructor , setters , getters  and w.e functions .

public static void main (String args[]){
try{    
 ObjectClass test = new ObjectClass("test",2);
 File f = new File("path to file");
 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
 out.writeObject(test); // this will write the object as it is onto the file
 out.close();
}catch(Exception ex){}

    }

 }  

您将无法读取其序列化的数据,但您可以将它们加载为如下对象:

ObjectInputStream in = new ObjectInputStream(new File("path to file"));
ObjectClass test =(ObjectClass) in.readObject(); // u have to cast from Object to Objectclass

答案 2 :(得分:0)

你可能想要的是ObjectOutputstream当porgram退出并使用相应的InputStreams读取Arraylist时,通过FileOutputStream将ArrayList写入文件。请参阅以下链接:

http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html