从用户输入写入java中的文件

时间:2018-04-11 14:21:09

标签: java file system writing

我正在尝试编写一个程序,该程序将从选项列表中获取用户输入,然后:

  • 写入文件
  • 更改现有文件或
  • 删除文件。

目前我仍然坚持只是从用户输入写入文件。我必须为每个用户输入使用正则表达式表示法,如代码段中所示。任何关于我能做什么的帮助或指导都将受到高度赞赏,是的,现在有很多错误。谢谢!

import java.io.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


private UserInput[] list;
class Input {
    public static void main(String[] args) {

        Scanner in = (System.in);
        string Exit ="False"; 
        System.out.println("person details");
        System.out.println("");
        System.out.println("Menu Options:");
        System.out.println("1. Add new person");
        System.out.println("2. Load person details ");
        System.out.println("3. Delete person Entry");
        System.out.print("Please select an option from 1-5\r\n");
        int choice = in.nextLine();


        if (choice == 1)
            system.out.println("you want to add a new person deails.");
            AddStudnet(); 


       else if (choice == 2){
            system.println("would you like to: ");
            system.println("1. Load a specific entry");
            system.println("2. Load ");


        }


//Error checking the options

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            int input = Integer.parseInt(br.readLine());

            if(input < 0 || input > 5) {
                System.out.println("You have entered an invalid selection, please try again\r\n");
            } else if(input == 5) {
                System.out.println("You have quit the program\r\n");
                System.exit(1);
            } else {
                System.out.println("You have entered " + input + "\r\n");
            }
        } catch (IOException ioe) {
            System.out.println("IO error trying to read your input!\r\n");
            System.exit(1);
        }
    }

}





//Scanner reader = new Scanner(System.in);{  // Reading from System.in
//System.out.println("Please enter your name: ");
//String n = reader.nextLine();
//}


  //      File file = new File("someFile.txt", True);
    //    FileWriter writer = new FileWriter(file);

      //  writer.write(reader);
//        writer.close();




public static void addPerson(String args[]) throws IOException{
    class input Per{

    scanner in = new scanner (system.in);
    system.out.print("Please enter you Name: ");
     String name = in.nextLine()
     final Pattern pattern = Pattern.compile("/^[a-z ,.'-]+$/i");
     if (!pattern.matcher(name).matches()) {
        throw new IllegalArgumentException("Invalid String");

     //String Name = regex ("Name")

     //regex below for formatting



    system.out.println("Please enter your Job title:");
    //String CourseNum = regex ("JobTitle");



    system.out.println("Please enter your Town:");
   //String Town = regex("Town");

    system.out.println("Please enter your postcocde:");
    //String postcocde = regex("postcocde");

    system.out.println("Please enter your street:");
    //String Street = regex ("Street");

    system.out.println("Please enter your House Number:");
    //String HouseNum = regex ("HouseNum");


    }

}


//public static void

1 个答案:

答案 0 :(得分:-1)

不清楚你是在寻找“我只是写在文件上”[/ p>

这里举例说明如何创建/写入文件

String export="/home/tata/test.txt";
if (!Files.exists(Paths.get(export)))
                Files.createDirectory(Paths.get(export));
List<String> toWrite = new ArrayList();
toWrite.add("tata");
Files.write(Paths.get(export),toWrite);

看看java tm https://docs.oracle.com/javase/tutorial/essential/io/file.html

LIO