Results 1 to 6 of 6
  1. #1
    nesooM is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default FileWriter only writes for some Files

    Hi
    So my code is supposed to be scanning either a website or a file, then finding certain substrings among the text and then storing that in a new file. The code works fine for getting the website and storing the website originally, but after I try to rewrite the text into another file it only sometimes works.

    I'm sure this will look messy on a forum, but here it is

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class webScan {
    	private File in;
    	private File result;
    public webScan(){
    	in = null;
    	result = null;
    }
    	public webScan(String fileName, String outFile) {
    		in = new File(fileName);
    		result = new File(outFile);
    	}
    //stores a new website into a file, this works
    	public void storeURL(String address) {
    		try {
    			URL url = new URL(address);// format like [url=http://www.google.com]Google[/url]
    			BufferedReader codeURL = new BufferedReader(new InputStreamReader(
    					url.openStream()));
    			FileWriter toFile = new FileWriter(in);
    			String codeLine = codeURL.readLine();
    			while (codeLine != null) {
    				toFile.write(codeLine + "\n"); // stores each line in file in
    				codeLine = codeURL.readLine();
    			}
    			toFile.flush(); // empties buffer into destination
    			toFile.close();
    		} catch (MalformedURLException me) {
    			System.out.println(me);
    
    		} catch (IOException ioe) {
    			System.out.println(ioe);
    		}
    	}
    // replaces substring with wanted string, works only sometimes
    	public void find(String target, String replace) {
    		Scanner readFile;
    		FileWriter toFile;
    		try {
    			readFile = new Scanner(in);
    			toFile = new FileWriter(result);
    			while (readFile.hasNextLine()) {
    				String line = readFile.nextLine();
    				if (line.contains(target)) {// checks file line by line for
    											// target
    					line = line.replaceAll(target, replace) + "\n";
    					toFile.write(line);
    				} else {
    					toFile.write(line + "\n");
    				}
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    //test cases
    	public static void main(String[] args) {
    		webScan a = new webScan("yes.txt", "output.txt");
    		a.storeURL("http://www.cs.colostate.edu/~cs161/Spring12/");
    		a.find("a", "b");
    
    	}
    }
    Moderator Edit: code tags added
    Last edited by Fubarable; 05-25-2012 at 11:47 PM. Reason: Mod edit: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: FileWriter only writes for some Files

    Can you explain what you mean by: does not work at all?
    Is a file created? Does it have some contents? How is the contents different from what you expected?

    Or is the problem with the logic (if statement) that is not true? Add some printlns to show what is being written to the file.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    nesooM is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: FileWriter only writes for some Files

    It's not a logic problem because the file is fully written for certain websites like yahoo and facebook. What is being written to the file is supposed to be a changed URL that was taken from the website. All the websites originally write to the first file, but after I make a change to the file, reading it in line by line and storing it to another new file, the code only sometimes works and I can't understand why.

    Also, the second file is created regardless of website or what is written in it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: FileWriter only writes for some Files

    code only sometimes works
    Please explain when and what happens that makes it work "sometimes". You need to show exactly what is happening.

    If there is no logic involved (like an if statement) the code will always be executed and something will be written.

    Did you add some printlns to the code to show what it is doing? What was printed out?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    nesooM is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: FileWriter only writes for some Files

    I added print lines to show what was happening and everything worked according to them, but the files made would end up being empty.

    The only way I can really show you is say I scanned in the phrase "blah blah blah" to a file and the file held it correctly. Next I changed all the Bs to As so it now read "alah alah alah" but the second file where the second phrase was to be stored, didn't have anything except for certain websites scanned.

    I explained what "sometimes" meant in my original problem by saying what happened for certain websites like:
    Facebook works
    Google does not work past 8 lines
    cs. colostate. edu/ ~cs192 does not work at all

    I think the problem lies in the line of code: toFile.write(line); but I cannot see why.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: FileWriter only writes for some Files

    the files made would end up being empty.
    Do you close the file after writing to it?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. File Writing Issue - Only Writes First Line
    By DragonGhola in forum Advanced Java
    Replies: 4
    Last Post: 07-22-2011, 04:41 PM
  2. Initializing FileWriter
    By loopsnhoops in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2011, 02:28 AM
  3. Replies: 0
    Last Post: 06-28-2010, 12:30 PM
  4. JAva Filewriter
    By tommyyyy in forum New To Java
    Replies: 1
    Last Post: 03-28-2009, 12:51 PM
  5. How can I make a loop, that writes a....
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 03-10-2009, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •