Problem to write on txt file on specific line










0















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();










share|improve this question
























  • 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















0















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();










share|improve this question
























  • 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













0












0








0








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();










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • 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










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
);



);













draft saved

draft discarded


















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









0














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.






share|improve this answer























  • 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















0














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.






share|improve this answer























  • 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













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 the if (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











  • How should i change the if (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

















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.




draft saved


draft discarded














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





















































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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế