d2jsp
d2jsp Forums > Off-Topic > Programmer's Haven > Java, Visual Basic, VBScript > Why Is My Code Not Working? :o
Add Reply New Topic New Poll
ferf
Group: Member
Posts: 30,635
Joined: Mar 25 2009
Gold: 50.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







azassault
Group: Member
Posts: 227
Joined: Jun 22 2020
Gold: 445.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.
azassault
Group: Member
Posts: 227
Joined: Jun 22 2020
Gold: 445.00
Jul 14 2020 11:19am
Download intellij and step through your code.
ferf
Group: Member
Posts: 30,635
Joined: Mar 25 2009
Gold: 50.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.
carteblanche
Group: Member
Posts: 32,696
Joined: Jul 23 2006
Gold: 1,080.00
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
ferf
Group: Member
Posts: 30,635
Joined: Mar 25 2009
Gold: 50.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!
voidqueen
Group: Member
Posts: 965
Joined: Aug 11 2019
Gold: 1,630.00
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.
lilGray1
Group: Member
Posts: 879
Joined: Jan 21 2007
Gold: 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 Java, Visual Basic, VBScript Topic List
Add Reply New Topic New Poll