Hi again,
Just thought I'd check with you guys to see where I've gone wrong with this simple I/O program. Having just done quite a bit of reading on I/O I thought would write myself I nice little program from scratch to test some of the said principles out. Sadly my output is locked within an infinite loop when I try reading from it.
The first code dump is the main prog, it validates whether there is an output txt file created, and if not creates one. Thus on it's 2nd execution it will instantiate an OutputCheck object + invoke it's checkOutput() method to see whether the output txt file originally created fits the bill. But it's an endless loop as previously mentioned.
import java.io.*; public class StringFileReadWrite { public static void main (String [] args){ File inFile = new File("src\\input.txt"); File outFile = new File("src\\output.txt"); if(!(outFile.exists())){ try{ FileReader fr = new FileReader(inFile); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(outFile); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); String line = br.readLine(); while(line != null){ pw.println(line); br.readLine(); } br.close(); pw.close(); System.out.println("The File Operation has been completed"); }catch(IOException e){ System.out.println(e); } } else{ OutputCheck oc = new OutputCheck(); oc.checkOutput(); } } }
import java.util.Scanner; import java.io.*; public class OutputCheck { File checked = new File("src\\output.txt"); public void checkOutput(){ try{ Scanner scan = new Scanner(checked); while(scan.hasNext()){ String str = scan.next(); System.out.println(str); } scan.close(); }catch(IOException e){ }finally{} } }