Reputation: 89
I want to create a small project where a name can be inserted and that is saved with the data of the input in a textfile. Additionally it should check, if this name is already in the document and if so, there should be an alert and the name should not be added.
The code below is how I add the names to the textfile.
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();
}
How can I do this in Java without jQuery/JavaScript?
Thank you for you help, Chris
Upvotes: 0
Views: 4834
Reputation: 1739
I'd suggest streaming line by line to reduce the memory footprint. This method does no validation and assumes that your file is formatted the way you suggested in your code.
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;
}
}
}
Upvotes: 0
Reputation: 1976
Hey you can do this like sample:
you have to follow these steps:
append it / else already exist
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();
}
}
}
}
Upvotes: 0
Reputation: 40118
Here is a simple solution: First build the name string, then read the file and check if it is in there. If it is alert, otherwise, perform your insert
// 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
}
Note that you must specify an encoding in the new String
constructor, unless your file uses your platform's default encoding. For example, it might look like this for UTF8:
String s = new String(bytes,StandardCharsets.UTF_8);
Also note that this approach reads the file into memory thoroughly. Thus, if you have a really huge file (>50 MB) that would yield an OutOfMemory
error upon reading, you should rather use a BufferedReader
and read the file line by line.
Upvotes: 3