Problem to write on txt file on specific line
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
After run my code I take this:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman,2,3,1,3,4,6
But I want, if I search for ID=2
go to 2nd line and put the numbers, like that:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman,2,3,1,3,4,6
3,Maria,Oaka,Woman
Code:
@FXML
TextField ID1,glossa,math,fis,xim,prog,gym;
@FXML
public void UseAddLesson() throws IOException
Scanner x = new Scanner("src/inware/students.txt");
FileWriter fW = new FileWriter("src/inware/students.txt",true);
BufferedWriter bW = new BufferedWriter(fW);
boolean found= false;
while(!found)
String line = x.nextLine();
if(line.contains(ID1.getText()))
bW.write(","+glossa.getText()+",");
bW.write(math.getText()+",");
bW.write(fis.getText()+",");
bW.write(xim.getText()+",");
bW.write(prog.getText()+",");
bW.write(gym.getText());
System.out.println(line);
found= true;
bW.close();
fW.close();
x.close();
java javafx text-files
add a comment |
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
After run my code I take this:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman,2,3,1,3,4,6
But I want, if I search for ID=2
go to 2nd line and put the numbers, like that:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman,2,3,1,3,4,6
3,Maria,Oaka,Woman
Code:
@FXML
TextField ID1,glossa,math,fis,xim,prog,gym;
@FXML
public void UseAddLesson() throws IOException
Scanner x = new Scanner("src/inware/students.txt");
FileWriter fW = new FileWriter("src/inware/students.txt",true);
BufferedWriter bW = new BufferedWriter(fW);
boolean found= false;
while(!found)
String line = x.nextLine();
if(line.contains(ID1.getText()))
bW.write(","+glossa.getText()+",");
bW.write(math.getText()+",");
bW.write(fis.getText()+",");
bW.write(xim.getText()+",");
bW.write(prog.getText()+",");
bW.write(gym.getText());
System.out.println(line);
found= true;
bW.close();
fW.close();
x.close();
java javafx text-files
are you sure thatID1.getText()
equals 2 ?
– Mohamed Ali RACHID
Nov 10 '18 at 22:13
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
okay , what shows this lineSystem.out.println(line);
?
– Mohamed Ali RACHID
Nov 10 '18 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37
add a comment |
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
After run my code I take this:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman,2,3,1,3,4,6
But I want, if I search for ID=2
go to 2nd line and put the numbers, like that:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman,2,3,1,3,4,6
3,Maria,Oaka,Woman
Code:
@FXML
TextField ID1,glossa,math,fis,xim,prog,gym;
@FXML
public void UseAddLesson() throws IOException
Scanner x = new Scanner("src/inware/students.txt");
FileWriter fW = new FileWriter("src/inware/students.txt",true);
BufferedWriter bW = new BufferedWriter(fW);
boolean found= false;
while(!found)
String line = x.nextLine();
if(line.contains(ID1.getText()))
bW.write(","+glossa.getText()+",");
bW.write(math.getText()+",");
bW.write(fis.getText()+",");
bW.write(xim.getText()+",");
bW.write(prog.getText()+",");
bW.write(gym.getText());
System.out.println(line);
found= true;
bW.close();
fW.close();
x.close();
java javafx text-files
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
After run my code I take this:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman,2,3,1,3,4,6
But I want, if I search for ID=2
go to 2nd line and put the numbers, like that:
Student.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman,2,3,1,3,4,6
3,Maria,Oaka,Woman
Code:
@FXML
TextField ID1,glossa,math,fis,xim,prog,gym;
@FXML
public void UseAddLesson() throws IOException
Scanner x = new Scanner("src/inware/students.txt");
FileWriter fW = new FileWriter("src/inware/students.txt",true);
BufferedWriter bW = new BufferedWriter(fW);
boolean found= false;
while(!found)
String line = x.nextLine();
if(line.contains(ID1.getText()))
bW.write(","+glossa.getText()+",");
bW.write(math.getText()+",");
bW.write(fis.getText()+",");
bW.write(xim.getText()+",");
bW.write(prog.getText()+",");
bW.write(gym.getText());
System.out.println(line);
found= true;
bW.close();
fW.close();
x.close();
java javafx text-files
java javafx text-files
edited Nov 10 '18 at 23:10
fabian
51.1k115272
51.1k115272
asked Nov 10 '18 at 22:09
FoticFotic
215
215
are you sure thatID1.getText()
equals 2 ?
– Mohamed Ali RACHID
Nov 10 '18 at 22:13
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
okay , what shows this lineSystem.out.println(line);
?
– Mohamed Ali RACHID
Nov 10 '18 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37
add a comment |
are you sure thatID1.getText()
equals 2 ?
– Mohamed Ali RACHID
Nov 10 '18 at 22:13
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
okay , what shows this lineSystem.out.println(line);
?
– Mohamed Ali RACHID
Nov 10 '18 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37
are you sure that
ID1.getText()
equals 2 ?– Mohamed Ali RACHID
Nov 10 '18 at 22:13
are you sure that
ID1.getText()
equals 2 ?– Mohamed Ali RACHID
Nov 10 '18 at 22:13
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
okay , what shows this line
System.out.println(line);
?– Mohamed Ali RACHID
Nov 10 '18 at 22:19
okay , what shows this line
System.out.println(line);
?– Mohamed Ali RACHID
Nov 10 '18 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
Do not attempt to read/write to the same file at the same time. You also cannot append/overwrite the structure of a text file requires all the text following the inserting point to be written at a different position.
I recommend creating a temporary file and replacing the old file with the new one you're done:
@FXML
public void UseAddLesson() throws IOException
String searchText = ID1.getText();
Path p = Paths.get("src", "inware", "students.txt");
Path tempFile = Files.createTempFile(p.getParent(), "studentsTemp", ".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(","+glossa.getText()+",");
writer.write(math.getText()+",");
writer.write(fis.getText()+",");
writer.write(xim.getText()+",");
writer.write(prog.getText()+",");
writer.write(gym.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);
Note: If you distribute the app, probably the src
directory will become unavailable.
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change theif (line.contains(searchText))
so i can delete lines?
– Fotic
Nov 11 '18 at 10:12
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243920%2fproblem-to-write-on-txt-file-on-specific-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do not attempt to read/write to the same file at the same time. You also cannot append/overwrite the structure of a text file requires all the text following the inserting point to be written at a different position.
I recommend creating a temporary file and replacing the old file with the new one you're done:
@FXML
public void UseAddLesson() throws IOException
String searchText = ID1.getText();
Path p = Paths.get("src", "inware", "students.txt");
Path tempFile = Files.createTempFile(p.getParent(), "studentsTemp", ".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(","+glossa.getText()+",");
writer.write(math.getText()+",");
writer.write(fis.getText()+",");
writer.write(xim.getText()+",");
writer.write(prog.getText()+",");
writer.write(gym.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);
Note: If you distribute the app, probably the src
directory will become unavailable.
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change theif (line.contains(searchText))
so i can delete lines?
– Fotic
Nov 11 '18 at 10:12
add a comment |
Do not attempt to read/write to the same file at the same time. You also cannot append/overwrite the structure of a text file requires all the text following the inserting point to be written at a different position.
I recommend creating a temporary file and replacing the old file with the new one you're done:
@FXML
public void UseAddLesson() throws IOException
String searchText = ID1.getText();
Path p = Paths.get("src", "inware", "students.txt");
Path tempFile = Files.createTempFile(p.getParent(), "studentsTemp", ".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(","+glossa.getText()+",");
writer.write(math.getText()+",");
writer.write(fis.getText()+",");
writer.write(xim.getText()+",");
writer.write(prog.getText()+",");
writer.write(gym.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);
Note: If you distribute the app, probably the src
directory will become unavailable.
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change theif (line.contains(searchText))
so i can delete lines?
– Fotic
Nov 11 '18 at 10:12
add a comment |
Do not attempt to read/write to the same file at the same time. You also cannot append/overwrite the structure of a text file requires all the text following the inserting point to be written at a different position.
I recommend creating a temporary file and replacing the old file with the new one you're done:
@FXML
public void UseAddLesson() throws IOException
String searchText = ID1.getText();
Path p = Paths.get("src", "inware", "students.txt");
Path tempFile = Files.createTempFile(p.getParent(), "studentsTemp", ".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(","+glossa.getText()+",");
writer.write(math.getText()+",");
writer.write(fis.getText()+",");
writer.write(xim.getText()+",");
writer.write(prog.getText()+",");
writer.write(gym.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);
Note: If you distribute the app, probably the src
directory will become unavailable.
Do not attempt to read/write to the same file at the same time. You also cannot append/overwrite the structure of a text file requires all the text following the inserting point to be written at a different position.
I recommend creating a temporary file and replacing the old file with the new one you're done:
@FXML
public void UseAddLesson() throws IOException
String searchText = ID1.getText();
Path p = Paths.get("src", "inware", "students.txt");
Path tempFile = Files.createTempFile(p.getParent(), "studentsTemp", ".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(","+glossa.getText()+",");
writer.write(math.getText()+",");
writer.write(fis.getText()+",");
writer.write(xim.getText()+",");
writer.write(prog.getText()+",");
writer.write(gym.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);
Note: If you distribute the app, probably the src
directory will become unavailable.
answered Nov 10 '18 at 23:39
fabianfabian
51.1k115272
51.1k115272
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change theif (line.contains(searchText))
so i can delete lines?
– Fotic
Nov 11 '18 at 10:12
add a comment |
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change theif (line.contains(searchText))
so i can delete lines?
– Fotic
Nov 11 '18 at 10:12
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic
Nov 11 '18 at 9:01
How should i change the
if (line.contains(searchText))
so i can delete lines?– Fotic
Nov 11 '18 at 10:12
How should i change the
if (line.contains(searchText))
so i can delete lines?– Fotic
Nov 11 '18 at 10:12
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243920%2fproblem-to-write-on-txt-file-on-specific-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
are you sure that
ID1.getText()
equals 2 ?– Mohamed Ali RACHID
Nov 10 '18 at 22:13
yes,because i take the 2nd txt output, i have put textbox so i can write ID number e.g. 2
– Fotic
Nov 10 '18 at 22:17
okay , what shows this line
System.out.println(line);
?– Mohamed Ali RACHID
Nov 10 '18 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic
Nov 10 '18 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 '18 at 22:37