d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Is My Code Not Working? :o
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Jul 13 2020 05:17pm
package com.ferfykins.EncryptDecrypt;

import java.io.*;

public class Test {


public static void main(String[] args) {

File file = new File("C:\\Users\\ferfers329\\Desktop\\Projects\\Ferfs-EncryptDecrypt\\TextInfo.txt");
try {
Encrypt(file);
} catch (IOException e) {
e.printStackTrace();
}


}


private static void Encrypt(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
FileWriter fr = new FileWriter(file);
String line = "";
int key = 5;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
// char[] chars = line.toCharArray();
// for (char c : chars) {
// c += key;
// fr.write(c);
// }

}


} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}


}



}









specifically:

while ((line = br.readLine()) != null) {
System.out.println(line);


part, it's not printing contents to console for some reason..... also deletes information from text file







Member
Posts: 562
Joined: Jun 22 2020
Gold: 888.00
Jul 14 2020 11:18am
Have you tried to set some break points in your IDE? I am guessing your string variable "line" is null and that is why it is not getting printed out. Also it is deleting everything in your file because you are reading and writing to the same place.
Member
Posts: 562
Joined: Jun 22 2020
Gold: 888.00
Jul 14 2020 11:19am
Download intellij and step through your code.
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Jul 14 2020 11:40am
Quote (azassault @ Jul 14 2020 12:18pm)
Have you tried to set some break points in your IDE? I am guessing your string variable "line" is null and that is why it is not getting printed out. Also it is deleting everything in your file because you are reading and writing to the same place.


ehh it's not string variable, tested with an altered code and it works fine...... (using different spot for filereader, but in the modified code i never close it or filewriter cuz it's out of scope)



but yes that's what i'm trying ot do, read the file and overwrite it line for line with new values.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 14 2020 02:07pm
Quote (ferf @ Jul 14 2020 01:40pm)

but yes that's what i'm trying ot do, read the file and overwrite it line for line with new values.


i would not recommend doing it that way. read the entire file first in memory, then write your file all at once. i assume you're not dealing with huge files
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Jul 14 2020 02:15pm
Quote (carteblanche @ Jul 14 2020 03:07pm)
i would not recommend doing it that way. read the entire file first in memory, then write your file all at once. i assume you're not dealing with huge files


Hmm alright thanks for tip!
Member
Posts: 1,367
Joined: Aug 11 2019
Gold: 91,307.78
Jul 18 2020 02:25pm
Quote (ferf @ Jul 14 2020 04:15pm)
Hmm alright thanks for tip!


Im assuming this issue is solved, if not DM me and i'll take a look.
Member
Posts: 879
Joined: Jan 21 2007
Gold: 2,275.00
Aug 4 2020 02:37pm
Quote (ferf @ Jul 13 2020 06:17pm)
package com.ferfykins.EncryptDecrypt;
while ((line = br.readLine()) != null)


line is an empty string and br.readLine() = null. So, they never equal.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll