簡體   English   中英

調用一個類的構造函數以修改它以在另一個類中使用

[英]Calling a constructor of a class to modify it to use in another class

該程序的目的是讀取文件並將其寫入文本文件。

為此,我有三個班級:

Class ReadFile //Reads and displays text from the text file

Class WriteFile //Gets input from user and puts it writes it to the text file 

Class Application //Holds the Main method for execution.

WriteFile類中,我有代碼可以將文本追加(添加)到文本文件,也可以不追加(刪除文本文件中的所有內容,然后將輸入寫入文本文件,這是通過此代碼完成的)

public class WriteFile 
{

private String path;
private boolean append_to_file = false;

public WriteFile(String file_path)
{
    path = file_path;
}

public WriteFile (String file_path, boolean append_value)
{
    path = file_path;
    append_to_file = append_value;
}

public void writeToFile(String textLine) throws IOException
{
    FileWriter write = new FileWriter(path);
    PrintWriter print_line = new PrintWriter(write);

    print_line.printf("%s" + "%n", textLine);
    print_line.close();
}
}

現在,決定是否添加數據到文本文件的唯一方法是注釋掉

append_to_file = append_value;

用戶當然不能做到這一點。 我想給用戶這個選項。

所以我想我可以在擁有代碼以接收輸入的Class Application中添加此代碼。

public class Application 
{

public static void main(String[] args) throws IOException
{



String file_name = "Q:/test.txt";

try
{

    ReadFile file = new ReadFile(file_name);
    String[] aryLines = file.OpenFile();

    for(int i = 0; i < aryLines.length; i++)
    {
        System.out.println(aryLines[i]);    
    }
}

catch (IOException e)
{
    System.out.println( e.getMessage());
}

//boolean End = false;
String userEnter;
Scanner input = new Scanner (System.in);

for(int i = 0; i < 10; i++)
{
System.out.println("\n\nenter text to write to file");
userEnter = input.nextLine();

WriteFile data = new WriteFile(file_name, true);
data.writeToFile( userEnter );

System.out.println( "Text File Written To" );
}

}
}

如何編寫代碼以使用戶可以選擇附加數據?

這是類ReadFile,如果有幫助

public class ReadFile 
{
private String path;

public ReadFile (String file_path)
{
    path = file_path;
}

public String[] OpenFile() throws IOException
{
    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = readLines();
    String[] textData = new String [numberOfLines];



    for(int i = 0; i < numberOfLines; i++)
    {
        textData[i] = textReader.readLine();
    }

    textReader.close();
    return textData;
}

int readLines() throws IOException
{
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while ((aLine = bf.readLine()) != null)
    {
        numberOfLines++;
    }
    bf.close();
    return numberOfLines;
}

}

我希望我清楚地解釋了這一點

謝謝凱爾

如果希望用戶始終附加或始終覆蓋一組特定的行,則可以執行以下操作:

System.out.println("Enter false to overwrite data, or true to append data");
userEnter = input.nextLine();
boolean append = Boolean.parseBoolean(userEnter);

如果將這些行放在循環外,則將詢問用戶一次,並且始終使用相同的值,或者可以將其放入循環內以每次詢問。

WriteFile data = new WriteFile(file_name, append);

您可以輕松使用Scanner向用戶詢問是否輸入Y/N 將此輸入轉換為boolean並將其傳遞給WriteFile構造函數。

因此,如果我不了解,則希望用戶能夠選擇是追加到文件還是覆蓋文件。

然后你可以做這樣的事情

String userEnter;
Scanner input = new Scanner (System.in);

for(int i = 0; i < 10; i++)
{
boolean append = true;
System.out.println("Want to overwrite file? (Y/N)");
if(input.nextLine().compareTo("Y")==0)
append=false

System.out.println("\n\nenter text to write to file");
userEnter = input.nextLine();

WriteFile data = new WriteFile(file_name, append);
data.writeToFile( userEnter );

System.out.println( "Text File Written To" );
}

現在,如果用戶在覆蓋問題上寫Y,則append_to_file將為false,否則為true。

這真的是基本的Java,希望對您有所幫助。

暫無
暫無

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

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