简体   繁体   English

从文本文件读取

[英]Reading from text files

I need to read data from a text file using Java and currently I am using: 我需要使用Java从文本文件中读取数据,目前正在使用:

public void read()  throws Exception 
{
    FileInputStream in = null;
    try 
    {
        Scanner input = new Scanner("dataset.txt");
        File inputFile = new File(fileName.nextLine());
        in = new FileInputStream(inputFile);
    } 
    catch ( Exception e) 
    {
        System.out.println(e);
    }
}

However, I wish to give the user a choice to read from dataset.txt and data.txt so I tried doing: 但是,我希望给用户一个选择读取dataset.txt和data.txt的选择,因此我尝试这样做:

public void read(int name)  throws Exception 
{
    FileInputStream in = null;
    try 
    {
        File inpu = new File(name.nextLine());
        in = new FileInputStream(input);
    } 
    catch ( Exception e) 
    {
        System.out.println(e);
    }

but I am getting an error : int cannot be dereferenced 但我遇到一个错误:不能取消引用int

fileName has type int . fileName类型为int You cannot call methods from primitive types. 您不能从原始类型调用方法。

Since you only have two different file, why don't you use an if statement to see which file the user wants to open and then give that file to your inputFile which you can pass to in . 由于只有两个不同的文件,所以为什么不使用if语句查看用户要打开的文件,然后将该文件提供给inputFile ,您可以将其传递给in

if (fileName == 1) {
    inputFile = new File("dataset.txt");
} else if (fileName == 2) {
    inputFile = new File("data.txt");
}

If "name" input variable is the value selected by the user is required to use a condition to select the according file name depending on the user decission 如果“名称”输入变量是用户选择的值,则需要根据用户的决定使用条件来选择相应的文件名

Example: 例:

public void read(int name)  throws Exception {
    FileInputStream in = null;
    try  {

        // File selection depending on user number selection
        String fileName="dataSet.txt";
        if(name == 2){
            fileName ="otherFileName.txt";
        }

        // InputStream creation
        File input = new File(fileName);
        in = new FileInputStream(input);

       // Your read operations here

    } catch ( Exception e) {
        System.out.println(e);
    }
}

On this example if the user press 2 the "otherFileName.txt" is used. 在此示例中,如果用户按2,则使用“ otherFileName.txt”。 Otherwise "dataSet.txt" is used. 否则,将使用“ dataSet.txt”。

Also al files are without path. 所有文件也没有路径。 These means that are required to be in the startup path of the application. 这些意味着必须位于应用程序的启动路径中。

First your code have a lot of errors. 首先,您的代码有很多错误。 I suppose if you are displaying this line on console 我想如果您在控制台上显示此行

System.out.print("Please choose a dataset (1) dataset.txt , (2)data.txt : ");

User's input is in int and you are passing like fileName.nextLine() , Which cause an error 用户的输入为int并且您像fileName.nextLine()这样传递,这会导致错误

try this: 尝试这个:

Scanner scan=new Scanner(System.in));
fileName="";
System.out.print("Please choose a dataset (1) dataset.txt , (2)data.txt : ");
int input = scan.nextInt();
if(input==1){
   fileName="dataset.txt";
}
if(input==2){
   fileName="data.txt";
}
File inputFile=new File(fileName));
... .

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

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