0

该程序的目的是将文件读取和写入文本文件。

为此,我有三个课程:

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;

这当然是用户无法做到的。我想给用户这个选项。

所以我想我可以在类应用程序中添加它,它包含接收输入的代码。

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" );
}

}
}

如何编写代码让用户可以选择附加数据?

如果有帮助,这里是 Class 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;
}

}

我希望我解释清楚

谢谢你凯尔

4

3 回答 3

1

如果您希望用户始终附加或始终覆盖特定的一组行,您可以执行以下操作:

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);
于 2013-09-24T07:25:01.063 回答
0

您可以轻松地使用Scanner询问用户Y/N输入。将此输入转换为 aboolean并将其传递给您的WriteFile构造函数。

于 2013-09-24T07:24:51.170 回答
0

因此,如果我理解您希望用户能够选择是追加到文件还是覆盖它。

然后你可以做这样的事情

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 将为假,否则为真。

这真的是基本的Java,我希望这会有所帮助。

于 2013-09-24T07:27:58.153 回答