简体   繁体   中英

How to create a txt file in Java?

I'm just want a program to register a user and then create a txt file to store there the information. I know it has to be with createNewFile method but I do not know how to use it. I'd try this in my code:

import java.util.*;

public class File{


public static void main(String args[]){
    Scanner sc = new Scanner(System.in);

byte option=0;

    do{
        System.out.println("\nMENU:\n");
        System.out.println("0.-EXIT");
        System.out.println("1.-REGISTER USER");
        System.out.println("\nPLEASE ENTER YOUR CHOICE:");
        option = sc.nextByte();
    }while(option!=0);

}//main
}//File

You can use a File object to create a new File an example is:

File createFile = new File("C:\\Users\\youruser\\desktop\\mynewfile.txt");
createFile.createNewFile();

If you want to read and write to the file you could use a PrintWriter or some other writing mechanism:

PrintWriter pw = new PrintWriter(createFile);

pw.write("File Contents");
//when you are done flush and close the pw
pw.flush();
pw.close();

If you need to append to the file you can do this:

PrintWriter pw = new PrintWriter(new FileOutputStream(createFile, true)); //true means append here

pw.append("File Contents");
//when you are done flush and close the pw
pw.flush();
pw.close();
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample {
    public static void main(String[] args) {
        try {

            String content = "This is the content to write into file";

            // File file = new File("/users/your_user_name/filename.txt");// unix case
            File file = new File("c:\\filename.txt"); //windows case

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Source: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

Ok so once you have the input from the user this is what you would use to write the username and password to a text file

         try {
        File file = new File("userInfo.txt");
        BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
              //set to true so you can add multiple users(it will append (false will create a new one everytime)) 

            output.write(username + "," + password);

        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

EDIT***

You can put all this in a method and call it every time you want to add the user

public void addUser(String username, String password){
        //my code from above ^^

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM