该程序的目的是读取和写入文件到文本文件。
为此,我有三个班级:
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
类中,我有代码要么将文本追加(添加)到文本文件中,要么不追加(删除文本文件中的所有内容,然后将输入写入文本文件,这是通过此代码完成的< / p>
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;
}
}
我希望我能清楚地解释这个
三江源 Keil的
答案 0 :(得分: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);
答案 1 :(得分:0)
您可以轻松使用Scanner
向用户询问Y/N
输入。将此输入转换为boolean
并将其传递给WriteFile
构造函数。
答案 2 :(得分: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将为false,否则将为真。
这是非常基本的Java,我希望这有帮助。