Can't Find File When Using Scanner to read In a Previously Created File

193 views Asked by At

I am trying to write a program that takes a document that was created with PrintWriter in a previous program and then sorts out the results. I had trouble getting the first program to run because I have an iMac and the path was not simply C://FileName.text. The first program does create the file and prints it out into a document. I can see it when I go to Finder and look under documents. However, trying to use the same format I used in the first program to locate the file does not seem to work. I need to be able to readIn this document created by he first program into the second program and then sort the results. I guess to simplify the question: I created a text file with one program, now I need to be able to use a second program to read in that document that was just created so I can sort out the results. I am not sure why the program can't locate the new file, I can see it in Documents.

This is the error it gives:

Exception in thread "main" java.io.FileNotFoundException: /Users/myName/user.home.UnsortedDocument.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at Sorting.readIn(Sorting.java:14)
at Sorting.main(Sorting.java:35)

This is what the document's title is:

user.home.UnsortedDocumentt.txt

Looking at the File properties, I can see it is saved in Java File. Is the problem it is not being saved in the correct place for the next program to find it?

Please help, I have tired to figure this out, but am stumped. I have included the codes to the two programs I have so far.

The first program that inputs one saved file and then scrambles it and prints out a scrambled document:

public Scramble(){
}

public void readIn()throws Exception {
    Scanner stdln = new Scanner(new File(System.getProperty("user.home"),"SortedDocument.txt"));
    int i = 0;
    while (stdln.hasNextLine()){
        line[i] = stdln.nextLine();
        i++;
    }
}

public void mixUp()throws Exception{
    PrintWriter out = new PrintWriter("user.home.UnsortedDocument.txt");
    int inc = 239;
    int counter = 0;

    while (inc>0){
        int r = rand.nextInt(inc);
        for (int i = 0; counter!=r; i++){
            if (line[i]!= null){
                counter++;
            }
            if (counter==r){
                out.println(line[i]);
                line[i] = null;
            }
        }
        counter = 0;
    }
    out.close();

    System.out.println("Your scrambled document has been written to UnsortedDocument.txt");   
}

Here is the part of the new program I am having trouble with, that part that reads in the newly created document:

import java.io.PrintWriter;
import java.io.File;
import java.util.*;
import java.text.*;

public class Sorting{

String[] line = new String[238];
    public reSort(){
}

public void readIn()throws Exception {
    Scanner stdln = new Scanner(new File(System.getProperty("user.home"),user.home.UnsortedDocument.txt"));
    int i = 0;
    while (stdln.hasNextLine()){
        line[i] = stdln.nextLine();
        i++;
    }
}
1

There are 1 answers

1
martinez314 On BEST ANSWER

You are not specifying a path with your use of PrintWriter. Try this:

PrintWriter out = new PrintWriter(new File(System.getProperty("user.home"),"user.home.UnsortedDocument.txt"));