我想创建一个小项目,其中可以插入名称,并将其与输入数据一起保存在文本文件中。 此外,它应该检查,如果该名称已经在文档中,如果是,则应该有警报并且不应该添加名称。
以下代码是我如何将名称添加到文本文件中。
else if (i == 1) {
Scanner input = new Scanner(System.in);
System.out.println("Surname: ");
surname = input.nextLine();
System.out.println("First name: ");
firstname = input.nextLine();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(sdf.format(date));
File file = new File("C://Users/Prakt1/Desktop/projektverwaltung.txt");
String content = ("Surname " + (surname) + LINE_SEPARATOR + "First name: " + (firstname) + LINE_SEPARATOR + "Added: " +
(sdf.format(date)) + LINE_SEPARATOR);
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C://Users/Prakt1/Desktop/projektverwaltung.txt", true)))) {
out.println(content);
}catch (IOException e) {
e.printStackTrace();
}
如果没有jQuery / JavaScript,我怎么能用Java做到这一点?
谢谢你的帮助, 克里斯
答案 0 :(得分:3)
这是一个简单的解决方案:首先构建名称字符串,然后读取文件并检查它是否在那里。如果是警报,否则,执行插入
// Build the string
String content = ("Surname " + (surname) + LINE_SEPARATOR + "First name: " + (firstname);
// Read all contents into a String
byte[] bytes = Files.readAllBytes(Paths.get("C://Users/Prakt1/Desktop/projektverwaltung.txt"));
String s = new String(bytes);
// Check if the name is contained
if(s.indexOf(content) != -1){
System.out.println("Name already present!");
} else {
... // Do your usual insertion
}
请注意,您必须在new String
构造函数中指定编码,除非您的文件使用平台的默认编码。例如,UTF8可能看起来像这样:
String s = new String(bytes,StandardCharsets.UTF_8);
另请注意,此方法会彻底将文件读入内存。因此,如果您有一个非常大的文件(> 50 MB),在阅读时会产生OutOfMemory
错误,您应该使用BufferedReader
并逐行读取文件。
答案 1 :(得分:0)
嘿,你可以这样做,例如:
您必须按照以下步骤操作:
追加/已存在
public void appendFile(String surName, String firstName){
File file = new File("C:/robots.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
String line = reader.readLine();
boolean isFound = false;
while(line != null){
System.out.println(line);
// or the match you want to e.g. surname
if(line.equals(surName)){
isFound = true;
break;
}
line = reader.readLine();
}
//This is End of File
if(!isFound)}{
//Not in file append it at the end of file
}else{
//Already exist in file
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
答案 2 :(得分:0)
我建议逐行流式传输以减少内存占用。此方法不进行验证,并假定您的文件格式与您在代码中建议的方式一致。
try( FileReader fileReader = new FileReader(
"C://Users/Prakt1/Desktop/projektverwaltung.txt" );
BufferedReader reader = new BufferedReader( fileReader ) )
{
String line = null;
String currentSurname = null;
String currentFirstName = null;
while( ( line = reader.readLine() ) != null )
{
if( line.startsWith( "Surname " ) )
{
currentSurname = line.substring( "Surname ".length() );
}
else if( line.startsWith( "First name: " ) )
{
currentFirstName = line.substring( "First name: ".length() );
}
else
{
if( currentSurname != null &&
currentFirstName != null &&
currentSurname.equals( surname ) &&
currentFirstName.equals( firstName ) )
{
System.out.println( "Name already in file." );
break;
}
currentSurname = null;
currentFirstName = null;
}
}
}