0

This one's a fun one. I'd appreciate any bit of help, and no previous stackoverflow questions are pointing me in the right location. Docs also weren't very helpful to me.

I'm being thrown a FileNotFoundException with this block of code:

    public static int wordOccurance(Word t, File D) throws FileNotFoundException
{
    int occurance = 0;

    BufferedReader mainReader = new BufferedReader(new FileReader(D));  
    Scanner kb = new Scanner(mainReader);

My tester file does not cause this to occur: ie. "File tester = new File("C:\read.txt");"

But the problem occurs solely when I pass a File constructed by this method:

public static File makeAndCombineFile(String FileOne, String FileTwo) throws IOException
{
    BufferedReader mainReader = null;// null holder value for the later useful bufferedReader
    Scanner kb = null;// null holder for later useful Scanner
    StringBuilder sb = new StringBuilder();

OTHER CONDITIONS BETWEEN THESE TWO CHUNKS. MOST LIKELY NOT PERTINENT. ALSO RETURN FILE, JUST IF ONE INPUT IS NULL.

else //in case both are good to go and obviously not null
    {
        mainReader = new BufferedReader(new FileReader(FileOne));
        kb = new Scanner(mainReader);
        while(kb.hasNext())
            sb.append(kb.nextLine() + "\n");
        mainReader = new BufferedReader(new FileReader(FileTwo));
        kb = new Scanner(mainReader);
        while(kb.hasNext())
            sb.append(kb.nextLine()+ "\n");

        kb.close();
        return new File(sb.toString());
    }
}
user3022479
  • 25
  • 1
  • 8

1 Answers1

1

It took me a while to figure out what was going on here, but it looks to me like you think that this line:

return new File(sb.toString());

creates a file on disk containing the text read from the two scanners. It does not. It creates a java.io.File, which is basically a representation of a file path; the path represented by this File is the data read from the scanners. In other words, if FileOne and FileTwo contained the text to War and Peace, then the path represented by that file would be the the text of War and Peace. The file will not have been created on disk; no data will have been written to it. You've just created an object that refers to a file that does not exist.

Use a FileWriter, perhaps in conjunction with a PrintWriter, to save the lines of text into a file; then you can create a File object containing the name of that file and pass it to your other routine:

PrintWriter pw = new PrintWriter(new FileWriter("somename.txt"));
while(kb.hasNext())
    pw.println(kb.nextLine());
pw.close();
return new File("somename.txt");
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Ah yes, I understand your thought process. However, I understand I am not writing to the disk. If FileOne and FileTwo are both copies of War and Peace, I just want the object(File) representation of the the two mashed together. Essentially just creating a object File with War and Peace in it twice. – user3022479 Nov 22 '13 at 16:41
  • Oh! I see what you mean now! Well, now that I'm on the right track I can't help but wonder why constructing a file from another file isn't allowed. It seems a little redundant though. – user3022479 Nov 22 '13 at 17:15