Problem to write on txt file on specific line
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 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 Pap
Nov 10 at 22:17
okay , what shows this lineSystem.out.println(line);?
– Mohamed Ali RACHID
Nov 10 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 23:10
fabian
48.9k114968
48.9k114968
asked Nov 10 at 22:09
Fotic Pap
95
95
are you sure thatID1.getText()equals 2 ?
– Mohamed Ali RACHID
Nov 10 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 Pap
Nov 10 at 22:17
okay , what shows this lineSystem.out.println(line);?
– Mohamed Ali RACHID
Nov 10 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37
add a comment |
are you sure thatID1.getText()equals 2 ?
– Mohamed Ali RACHID
Nov 10 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 Pap
Nov 10 at 22:17
okay , what shows this lineSystem.out.println(line);?
– Mohamed Ali RACHID
Nov 10 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37
are you sure that
ID1.getText() equals 2 ?– Mohamed Ali RACHID
Nov 10 at 22:13
are you sure that
ID1.getText() equals 2 ?– Mohamed Ali RACHID
Nov 10 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 Pap
Nov 10 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 Pap
Nov 10 at 22:17
okay , what shows this line
System.out.println(line); ?– Mohamed Ali RACHID
Nov 10 at 22:19
okay , what shows this line
System.out.println(line); ?– Mohamed Ali RACHID
Nov 10 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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 Pap
Nov 11 at 9:01
How should i change theif (line.contains(searchText))so i can delete lines?
– Fotic Pap
Nov 11 at 10:12
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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 Pap
Nov 11 at 9:01
How should i change theif (line.contains(searchText))so i can delete lines?
– Fotic Pap
Nov 11 at 10:12
add a comment |
up vote
0
down vote
accepted
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 Pap
Nov 11 at 9:01
How should i change theif (line.contains(searchText))so i can delete lines?
– Fotic Pap
Nov 11 at 10:12
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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 at 23:39
fabian
48.9k114968
48.9k114968
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic Pap
Nov 11 at 9:01
How should i change theif (line.contains(searchText))so i can delete lines?
– Fotic Pap
Nov 11 at 10:12
add a comment |
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic Pap
Nov 11 at 9:01
How should i change theif (line.contains(searchText))so i can delete lines?
– Fotic Pap
Nov 11 at 10:12
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic Pap
Nov 11 at 9:01
I have try the given code, and it work perfectly. Thanks very much for the help
– Fotic Pap
Nov 11 at 9:01
How should i change the
if (line.contains(searchText)) so i can delete lines?– Fotic Pap
Nov 11 at 10:12
How should i change the
if (line.contains(searchText)) so i can delete lines?– Fotic Pap
Nov 11 at 10:12
add a comment |
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 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 Pap
Nov 10 at 22:17
okay , what shows this line
System.out.println(line);?– Mohamed Ali RACHID
Nov 10 at 22:19
for e.g ID=2, Print Ourput: 2,Maria,Karra,Woman and i take the 2rd txt output
– Fotic Pap
Nov 10 at 22:22
check my answer
– Mohamed Ali RACHID
Nov 10 at 22:37