簡體   English   中英

Java是否從用戶輸入中查找/讀取文件?

[英]Have java find/read a file from user input?

該程序必須在我要求讀取的文件中打印文本。 問題是我將其編碼為從特定文件中讀取文本

 File file = new File("numbersonnumbers.txt");
 Scanner inputFile = new Scanner(file);

如何使程序從用戶輸入指定的文件中讀取文本? *該文件將與該程序位於同一目錄/文件夾中,所以這不是問題。

編輯:我以前的用戶輸入嘗試

Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);

聲明一個String變量,例如someUserFile ,接受用戶輸入。 然后...

File file = new File(someUserFile);
Scanner inputFile = new Scanner(file);

您可能要檢查他們的輸入,並將".txt"附加到他們的輸入末尾。

如果可行:

File file = new File("numbersonnumbers.txt");
Scanner inputFile = new Scanner(file);

然后這樣:

Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);

假設您鍵入numbersonnumbers.txt並准確鍵入,將做完全相同的事情。

假設您准確鍵入numbersonnumbers.txt ,則第二個代碼段與第一個代碼段完全相同。

首先,您應該要求用戶輸入文件名。 然后,只需將代碼中的“ numbersonnumbers.txt”替換為您為用戶輸入設置的變量即可。

Scanner in = new Scanner(System.in);
System.out.println("What is the filename?");
String input = in.nextLine();
File file = new File(input);
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);

1)程序必須打印文件中的文本:

  • 讓我們將此文件稱為dataFile.textdataFile.dat (兩種方法均可

  • 假設客戶端將輸入任何文件,我們必須獲取文件名...

    這些Java語句可以解決問題:

    //創建掃描程序以讀取用戶的文件名

  • 掃描儀userInput =新的Scanner(System.in);

    //向客戶端請求文件名

  • System.out.println(“輸入輸入文件的名稱:”);

    //獲取客戶端的文件名

  • 字符串fileName = scanFile.nextLine();

    // scanFile,掃描位於src目錄中的新File fileName(客戶端文件)。 “ src /”代表文件(客戶端文件)的路徑。

  • 掃描程序scanFile = new Scanner(new File(“ src /” + fileName));

    • 由於問題表明文件“將與程序位於同一目錄中”,因此這不是問題。

-------------------------------------------------- -------------------------------

2)問題是我將其編碼為從特定文件中讀取文本

  • 是的,您所做的只是創建自己的名為“ numbersonnumbers.txt”的文件,這正是您在解決此問題時應遵循的代碼,但是您將使用而不是鍵入“ numbersonnumbers.text”客戶的輸入文件,它是先前代碼中的字符串變量“ fileName” ... *

    File file = new File(“ numbersonnumbers.txt”);

    掃描儀inputFile =新的Scanner(file);

已更正:File file = new File(“ src /” + fileName);

掃描儀inputFile =新的Scanner(file); //將掃描所有文件的數據

更正后的內容:掃描儀inputFile =新掃描儀(新File(“ src /” + fileName));

-------------------------------------------------- -------------------------------

3)程序必須打印文件中的文本

  • 現在,您只需將文件中的數據項打印到控制台,即可根據實現規范將文件中的每個項復制到數組,列表或樹中,但這顯然是初學者編程,因此,不必擔心列表和樹,使用數組。

因此,您已經完成了#1中的Scanner scanFile或#2中的Scanner inputFile。

現在,創建一個數組,顯然數組的類型將基於文件中數據項的類型...字符串(例如,月份名稱)或int(例如,ID號..ect)

假設文件中的數據項是月份的名稱(jan,feb,march),我們將創建一個String類型的數組。

String [] fileData = new String [100]; //具有隨機長度(100)

//現在,我們將文件中的每個項目復制到數組中,您將需要占位符值以在遍歷(在每個項目處停止)文件的項目期間跟蹤位置。

int i = 0; //遍歷遍歷的開始(文件項的索引0)

while(scanFile.hasNextLine()){ //當文件在下一行輸入時,從文件中讀取。

    fileData[++i] = scanFile.nextLine(); // Input file data and increment i

    System.out.println(fileData[i]); // Prints all of the file's data items

    } // end while

恭喜-!di0m

您已經在代碼中獲得了幾乎所有需要的東西。 您只需要正確地重新排列代碼行,並添加更多代碼即可。

您需要執行兩個步驟:掃描用戶作為完整文件路徑輸入的內容,然后按如下所示掃描實際文件數據:

// Scanner Object to be used for the user file path input
Scanner keyboard = new Scanner(System.in);

// Asks the user to enter the full path of their file
System.out.print("Enter your full file path: ");

// Stores the user input file path as a string
String filename = keyboard.nextLine();

// Creates a File object for the file path, and scans it
Scanner inputFile = new Scanner(new File(filename);    

現在,您可以閱讀掃描的文件inputFile 例如,您可以使用while循環一次讀取一行,以讀取所有數據並按以下方式打印出來:

// Reads one line at a time, and prints it out
while(inputFile.hasNext) {
    System.out.println(inputFile.nextLine());

或者,您可以將每行的原始數據(已修剪和拆分)放入數組中,並按如下所示將每行打印為數組:

// Declares a String array[] to be used for each line
String[] rawData;    

// Reads one line at a time, trims and split the raw data,
// and fills up the array with raw data elements, then prints 
// out the array. it repeats the same thing for each line.
while (scanner2.hasNext()) {
    rawData = scanner2.nextLine().trim().split("\\s+");
    System.out.println(Arrays.toString(rawData));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM