How to update a txt file on java











up vote
0
down vote

favorite












I have a text file and I want to update everytime only the last 3 labels



The changes will be made via ID (1,2 etc.)



p.g. (for ID=1 change: 10,18/10/18,7 to 12,21/10/18,2)



User.txt:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


So after run i want to take this as output:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12,21/10/18,2
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


The code searches for ID, reads-writes data but how can i replace the last 3 labels everytime?



Source Code:



    @FXML 
TextField ID3,abs,Dabs,Fabs
@FXML
public void UseAddAbs() throws IOException { //Kanei Add tis Apousies sinfona me to ID
String searchText = ID3.getText();
Path p = Paths.get("src", "inware", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
writer.write(line);
if (line.contains(searchText)) {
writer.write("," + Fabs.getText());
writer.write("," + Dabs.getText());
writer.write("," + abs.getText());
break;
}
writer.newLine();
}

// copy remaining lines
if (line != null) {
writer.newLine();
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
}









share|improve this question
























  • what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
    – Stultuske
    Nov 12 at 8:13










  • i make it on javafx to read write on file via ui
    – Fotic
    Nov 12 at 8:19










  • i paste my code
    – Fotic
    Nov 12 at 8:19










  • Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
    – Stultuske
    Nov 12 at 8:21










  • but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
    – Fotic
    Nov 12 at 8:21















up vote
0
down vote

favorite












I have a text file and I want to update everytime only the last 3 labels



The changes will be made via ID (1,2 etc.)



p.g. (for ID=1 change: 10,18/10/18,7 to 12,21/10/18,2)



User.txt:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


So after run i want to take this as output:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12,21/10/18,2
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


The code searches for ID, reads-writes data but how can i replace the last 3 labels everytime?



Source Code:



    @FXML 
TextField ID3,abs,Dabs,Fabs
@FXML
public void UseAddAbs() throws IOException { //Kanei Add tis Apousies sinfona me to ID
String searchText = ID3.getText();
Path p = Paths.get("src", "inware", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
writer.write(line);
if (line.contains(searchText)) {
writer.write("," + Fabs.getText());
writer.write("," + Dabs.getText());
writer.write("," + abs.getText());
break;
}
writer.newLine();
}

// copy remaining lines
if (line != null) {
writer.newLine();
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
}









share|improve this question
























  • what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
    – Stultuske
    Nov 12 at 8:13










  • i make it on javafx to read write on file via ui
    – Fotic
    Nov 12 at 8:19










  • i paste my code
    – Fotic
    Nov 12 at 8:19










  • Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
    – Stultuske
    Nov 12 at 8:21










  • but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
    – Fotic
    Nov 12 at 8:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a text file and I want to update everytime only the last 3 labels



The changes will be made via ID (1,2 etc.)



p.g. (for ID=1 change: 10,18/10/18,7 to 12,21/10/18,2)



User.txt:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


So after run i want to take this as output:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12,21/10/18,2
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


The code searches for ID, reads-writes data but how can i replace the last 3 labels everytime?



Source Code:



    @FXML 
TextField ID3,abs,Dabs,Fabs
@FXML
public void UseAddAbs() throws IOException { //Kanei Add tis Apousies sinfona me to ID
String searchText = ID3.getText();
Path p = Paths.get("src", "inware", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
writer.write(line);
if (line.contains(searchText)) {
writer.write("," + Fabs.getText());
writer.write("," + Dabs.getText());
writer.write("," + abs.getText());
break;
}
writer.newLine();
}

// copy remaining lines
if (line != null) {
writer.newLine();
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
}









share|improve this question















I have a text file and I want to update everytime only the last 3 labels



The changes will be made via ID (1,2 etc.)



p.g. (for ID=1 change: 10,18/10/18,7 to 12,21/10/18,2)



User.txt:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


So after run i want to take this as output:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12,21/10/18,2
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


The code searches for ID, reads-writes data but how can i replace the last 3 labels everytime?



Source Code:



    @FXML 
TextField ID3,abs,Dabs,Fabs
@FXML
public void UseAddAbs() throws IOException { //Kanei Add tis Apousies sinfona me to ID
String searchText = ID3.getText();
Path p = Paths.get("src", "inware", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
writer.write(line);
if (line.contains(searchText)) {
writer.write("," + Fabs.getText());
writer.write("," + Dabs.getText());
writer.write("," + abs.getText());
break;
}
writer.newLine();
}

// copy remaining lines
if (line != null) {
writer.newLine();
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
}






java javafx file-io text-files






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:48









Bsquare

2,23511029




2,23511029










asked Nov 12 at 8:12









Fotic

215




215












  • what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
    – Stultuske
    Nov 12 at 8:13










  • i make it on javafx to read write on file via ui
    – Fotic
    Nov 12 at 8:19










  • i paste my code
    – Fotic
    Nov 12 at 8:19










  • Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
    – Stultuske
    Nov 12 at 8:21










  • but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
    – Fotic
    Nov 12 at 8:21


















  • what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
    – Stultuske
    Nov 12 at 8:13










  • i make it on javafx to read write on file via ui
    – Fotic
    Nov 12 at 8:19










  • i paste my code
    – Fotic
    Nov 12 at 8:19










  • Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
    – Stultuske
    Nov 12 at 8:21










  • but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
    – Fotic
    Nov 12 at 8:21
















what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
– Stultuske
Nov 12 at 8:13




what have you tried so far, and what doesn't work? so far, you've just copied your assignment here, that isn't a very constructive way to ask to improve what you have
– Stultuske
Nov 12 at 8:13












i make it on javafx to read write on file via ui
– Fotic
Nov 12 at 8:19




i make it on javafx to read write on file via ui
– Fotic
Nov 12 at 8:19












i paste my code
– Fotic
Nov 12 at 8:19




i paste my code
– Fotic
Nov 12 at 8:19












Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
– Stultuske
Nov 12 at 8:21




Code: thanks to Fabian. so go and ask Fabian, if you are just posting his code anyway.
– Stultuske
Nov 12 at 8:21












but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
– Fotic
Nov 12 at 8:21




but on this version of code just go to end of the line and paste the new labels but i want to replace the last 3 labels (10,18/10/18,7 to 12,21/10/18,2)
– Fotic
Nov 12 at 8:21












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Split the lines and work on fields:



        while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (fields[0].equals(searchID)) {
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.println(String.join(",", fields));
}


Update: my code:



public static void main(String args) {
try {
String searchText = "1";
Path p = Paths.get(".", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (searchText.equals(fields[0])) {
for (int i = 0; i < fields.length; ++i) {
System.out.println(i + ": " + fields[i]);
}
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.write(String.join(",", fields));
writer.newLine();
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}





share|improve this answer























  • Thanks, I have try the solution. but it delete all the file content
    – Fotic
    Nov 12 at 9:27












  • @FoticPap no, it doesn't
    – Maurice Perry
    Nov 12 at 10:04










  • dont know, but is not working for me, can complete the solution? Thanks
    – Fotic
    Nov 12 at 10:25










  • @FoticPap here you go
    – Maurice Perry
    Nov 12 at 13:08










  • It's working Perfect, Thanks very much!!!
    – Fotic
    Nov 12 at 14:15


















up vote
0
down vote













You can split line into array and update last 3 array elements.
Then all you have to do is build new line from that array and write this line instead of an old one.



Example:.



while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) {
String updatedLine = updateLastNStrings(line, 3);
writer.write(updatedLine);
break;
} else {
writer.write(line);
}
writer.newLine();
}

private String updateLastNStrings(String line, int n) {
String parts = line.split(",");
for (int i = parts.length - n; i < parts.length; i++) {
// update String
String newPart = parts[i] + "_UPDATED";
parts[i] = newPart;
}
return String.join(",", parts);
}


Input:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Result



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12_UPDATED,21/10/18_UPDATED,2_UPDATED
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Note updated String with _UPDATED



Note



Besides all that, the code you found is flawed, since to find ID it is searching if line contains a charSequence. What if ID is a content of a line?



Example:



1,some_content,2,3
2,some_content,1


You try to find a line by id 2 and first line will be updated instead of a second one.






share|improve this answer























  • Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
    – Fotic
    Nov 12 at 8:55










  • I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
    – Fotic
    Nov 12 at 9:08










  • What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
    – CrazySabbath
    Nov 12 at 9:11










  • I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
    – Fotic
    Nov 12 at 9:21












  • What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
    – CrazySabbath
    Nov 12 at 11:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258095%2fhow-to-update-a-txt-file-on-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Split the lines and work on fields:



        while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (fields[0].equals(searchID)) {
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.println(String.join(",", fields));
}


Update: my code:



public static void main(String args) {
try {
String searchText = "1";
Path p = Paths.get(".", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (searchText.equals(fields[0])) {
for (int i = 0; i < fields.length; ++i) {
System.out.println(i + ": " + fields[i]);
}
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.write(String.join(",", fields));
writer.newLine();
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}





share|improve this answer























  • Thanks, I have try the solution. but it delete all the file content
    – Fotic
    Nov 12 at 9:27












  • @FoticPap no, it doesn't
    – Maurice Perry
    Nov 12 at 10:04










  • dont know, but is not working for me, can complete the solution? Thanks
    – Fotic
    Nov 12 at 10:25










  • @FoticPap here you go
    – Maurice Perry
    Nov 12 at 13:08










  • It's working Perfect, Thanks very much!!!
    – Fotic
    Nov 12 at 14:15















up vote
1
down vote



accepted










Split the lines and work on fields:



        while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (fields[0].equals(searchID)) {
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.println(String.join(",", fields));
}


Update: my code:



public static void main(String args) {
try {
String searchText = "1";
Path p = Paths.get(".", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (searchText.equals(fields[0])) {
for (int i = 0; i < fields.length; ++i) {
System.out.println(i + ": " + fields[i]);
}
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.write(String.join(",", fields));
writer.newLine();
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}





share|improve this answer























  • Thanks, I have try the solution. but it delete all the file content
    – Fotic
    Nov 12 at 9:27












  • @FoticPap no, it doesn't
    – Maurice Perry
    Nov 12 at 10:04










  • dont know, but is not working for me, can complete the solution? Thanks
    – Fotic
    Nov 12 at 10:25










  • @FoticPap here you go
    – Maurice Perry
    Nov 12 at 13:08










  • It's working Perfect, Thanks very much!!!
    – Fotic
    Nov 12 at 14:15













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Split the lines and work on fields:



        while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (fields[0].equals(searchID)) {
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.println(String.join(",", fields));
}


Update: my code:



public static void main(String args) {
try {
String searchText = "1";
Path p = Paths.get(".", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (searchText.equals(fields[0])) {
for (int i = 0; i < fields.length; ++i) {
System.out.println(i + ": " + fields[i]);
}
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.write(String.join(",", fields));
writer.newLine();
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}





share|improve this answer














Split the lines and work on fields:



        while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (fields[0].equals(searchID)) {
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.println(String.join(",", fields));
}


Update: my code:



public static void main(String args) {
try {
String searchText = "1";
Path p = Paths.get(".", "users.txt");
Path tempFile = Files.createTempFile(p.getParent(), "usersTemp", ".txt");
try (BufferedReader reader = Files.newBufferedReader(p);
BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
String line;

// copy everything until the id is found
while ((line = reader.readLine()) != null) {
String fields = line.split("[,]");
if (searchText.equals(fields[0])) {
for (int i = 0; i < fields.length; ++i) {
System.out.println(i + ": " + fields[i]);
}
fields[12] = "12";
fields[13] = "21/10/18";
fields[14] = "2";
}
writer.write(String.join(",", fields));
writer.newLine();
}
}

// copy new file & delete temporary file
Files.copy(tempFile, p, StandardCopyOption.REPLACE_EXISTING);
Files.delete(tempFile);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 13:06

























answered Nov 12 at 9:10









Maurice Perry

4,6752415




4,6752415












  • Thanks, I have try the solution. but it delete all the file content
    – Fotic
    Nov 12 at 9:27












  • @FoticPap no, it doesn't
    – Maurice Perry
    Nov 12 at 10:04










  • dont know, but is not working for me, can complete the solution? Thanks
    – Fotic
    Nov 12 at 10:25










  • @FoticPap here you go
    – Maurice Perry
    Nov 12 at 13:08










  • It's working Perfect, Thanks very much!!!
    – Fotic
    Nov 12 at 14:15


















  • Thanks, I have try the solution. but it delete all the file content
    – Fotic
    Nov 12 at 9:27












  • @FoticPap no, it doesn't
    – Maurice Perry
    Nov 12 at 10:04










  • dont know, but is not working for me, can complete the solution? Thanks
    – Fotic
    Nov 12 at 10:25










  • @FoticPap here you go
    – Maurice Perry
    Nov 12 at 13:08










  • It's working Perfect, Thanks very much!!!
    – Fotic
    Nov 12 at 14:15
















Thanks, I have try the solution. but it delete all the file content
– Fotic
Nov 12 at 9:27






Thanks, I have try the solution. but it delete all the file content
– Fotic
Nov 12 at 9:27














@FoticPap no, it doesn't
– Maurice Perry
Nov 12 at 10:04




@FoticPap no, it doesn't
– Maurice Perry
Nov 12 at 10:04












dont know, but is not working for me, can complete the solution? Thanks
– Fotic
Nov 12 at 10:25




dont know, but is not working for me, can complete the solution? Thanks
– Fotic
Nov 12 at 10:25












@FoticPap here you go
– Maurice Perry
Nov 12 at 13:08




@FoticPap here you go
– Maurice Perry
Nov 12 at 13:08












It's working Perfect, Thanks very much!!!
– Fotic
Nov 12 at 14:15




It's working Perfect, Thanks very much!!!
– Fotic
Nov 12 at 14:15












up vote
0
down vote













You can split line into array and update last 3 array elements.
Then all you have to do is build new line from that array and write this line instead of an old one.



Example:.



while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) {
String updatedLine = updateLastNStrings(line, 3);
writer.write(updatedLine);
break;
} else {
writer.write(line);
}
writer.newLine();
}

private String updateLastNStrings(String line, int n) {
String parts = line.split(",");
for (int i = parts.length - n; i < parts.length; i++) {
// update String
String newPart = parts[i] + "_UPDATED";
parts[i] = newPart;
}
return String.join(",", parts);
}


Input:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Result



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12_UPDATED,21/10/18_UPDATED,2_UPDATED
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Note updated String with _UPDATED



Note



Besides all that, the code you found is flawed, since to find ID it is searching if line contains a charSequence. What if ID is a content of a line?



Example:



1,some_content,2,3
2,some_content,1


You try to find a line by id 2 and first line will be updated instead of a second one.






share|improve this answer























  • Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
    – Fotic
    Nov 12 at 8:55










  • I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
    – Fotic
    Nov 12 at 9:08










  • What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
    – CrazySabbath
    Nov 12 at 9:11










  • I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
    – Fotic
    Nov 12 at 9:21












  • What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
    – CrazySabbath
    Nov 12 at 11:24















up vote
0
down vote













You can split line into array and update last 3 array elements.
Then all you have to do is build new line from that array and write this line instead of an old one.



Example:.



while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) {
String updatedLine = updateLastNStrings(line, 3);
writer.write(updatedLine);
break;
} else {
writer.write(line);
}
writer.newLine();
}

private String updateLastNStrings(String line, int n) {
String parts = line.split(",");
for (int i = parts.length - n; i < parts.length; i++) {
// update String
String newPart = parts[i] + "_UPDATED";
parts[i] = newPart;
}
return String.join(",", parts);
}


Input:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Result



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12_UPDATED,21/10/18_UPDATED,2_UPDATED
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Note updated String with _UPDATED



Note



Besides all that, the code you found is flawed, since to find ID it is searching if line contains a charSequence. What if ID is a content of a line?



Example:



1,some_content,2,3
2,some_content,1


You try to find a line by id 2 and first line will be updated instead of a second one.






share|improve this answer























  • Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
    – Fotic
    Nov 12 at 8:55










  • I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
    – Fotic
    Nov 12 at 9:08










  • What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
    – CrazySabbath
    Nov 12 at 9:11










  • I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
    – Fotic
    Nov 12 at 9:21












  • What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
    – CrazySabbath
    Nov 12 at 11:24













up vote
0
down vote










up vote
0
down vote









You can split line into array and update last 3 array elements.
Then all you have to do is build new line from that array and write this line instead of an old one.



Example:.



while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) {
String updatedLine = updateLastNStrings(line, 3);
writer.write(updatedLine);
break;
} else {
writer.write(line);
}
writer.newLine();
}

private String updateLastNStrings(String line, int n) {
String parts = line.split(",");
for (int i = parts.length - n; i < parts.length; i++) {
// update String
String newPart = parts[i] + "_UPDATED";
parts[i] = newPart;
}
return String.join(",", parts);
}


Input:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Result



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12_UPDATED,21/10/18_UPDATED,2_UPDATED
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Note updated String with _UPDATED



Note



Besides all that, the code you found is flawed, since to find ID it is searching if line contains a charSequence. What if ID is a content of a line?



Example:



1,some_content,2,3
2,some_content,1


You try to find a line by id 2 and first line will be updated instead of a second one.






share|improve this answer














You can split line into array and update last 3 array elements.
Then all you have to do is build new line from that array and write this line instead of an old one.



Example:.



while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) {
String updatedLine = updateLastNStrings(line, 3);
writer.write(updatedLine);
break;
} else {
writer.write(line);
}
writer.newLine();
}

private String updateLastNStrings(String line, int n) {
String parts = line.split(",");
for (int i = parts.length - n; i < parts.length; i++) {
// update String
String newPart = parts[i] + "_UPDATED";
parts[i] = newPart;
}
return String.join(",", parts);
}


Input:



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,10,18/10/18,7
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Result



1,alex,pass,Admin,Alexandros,Karatzas,Male,18/10/1990,A'Likeiou,Antreas,Karatzas,6945952301,12_UPDATED,21/10/18_UPDATED,2_UPDATED
2,maria,pass1,User,Maria,Karatza,Female,18/10/1990,B'Likeiou,Antreas,Karatzas,6945952381,20,18/10/18,5


Note updated String with _UPDATED



Note



Besides all that, the code you found is flawed, since to find ID it is searching if line contains a charSequence. What if ID is a content of a line?



Example:



1,some_content,2,3
2,some_content,1


You try to find a line by id 2 and first line will be updated instead of a second one.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 8:53

























answered Nov 12 at 8:48









CrazySabbath

7401725




7401725












  • Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
    – Fotic
    Nov 12 at 8:55










  • I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
    – Fotic
    Nov 12 at 9:08










  • What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
    – CrazySabbath
    Nov 12 at 9:11










  • I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
    – Fotic
    Nov 12 at 9:21












  • What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
    – CrazySabbath
    Nov 12 at 11:24


















  • Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
    – Fotic
    Nov 12 at 8:55










  • I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
    – Fotic
    Nov 12 at 9:08










  • What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
    – CrazySabbath
    Nov 12 at 9:11










  • I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
    – Fotic
    Nov 12 at 9:21












  • What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
    – CrazySabbath
    Nov 12 at 11:24
















Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
– Fotic
Nov 12 at 8:55




Thanks for the help, i didnt notice that, i will chage the IDs to 1000+
– Fotic
Nov 12 at 8:55












I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
– Fotic
Nov 12 at 9:08




I try the solution it works, but it throws IO.Exception and is not replace the the users file just create a usersTemp with the changes
– Fotic
Nov 12 at 9:08












What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
– CrazySabbath
Nov 12 at 9:11




What does the exception say? You might want to close buffers before copying new file writer.close(); reader.close();
– CrazySabbath
Nov 12 at 9:11












I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
– Fotic
Nov 12 at 9:21






I see it closely, create a temp file with the old data + _Update. The massage is so long i cant even post it :P
– Fotic
Nov 12 at 9:21














What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
– CrazySabbath
Nov 12 at 11:24




What do you mean you see it closely? You don't have to copy whole stacktrace....copy message and first few lines.
– CrazySabbath
Nov 12 at 11:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258095%2fhow-to-update-a-txt-file-on-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly