Results 1 to 18 of 18
  1. #1
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Help with reading input file

    So i am working on a stock analysis program and i am writing to a text document inorder to save a list of all the stocks that i am downloading data for. I wrote a class call ReadFiles which reads the .txt mentioned above and another file that stores the data. The data file is a .csv (a text document in which each element is seperated by a comma).

    So my problem is I can't get it to find the .txt document. It has no problem reading the .csv and I wrote the .txt reading method the exact same way. I am always getting the "#ReadFile# - - - This is not a file" error that i wrote into my code (see below).

    An example .txt file would look like this:

    Dow Jones, DOW
    General ELectric, GE
    Here is my code
    Java Code:
    /**
     * @(#)ReadFile.java
     *
     *
     * @Mike Genovese
     * @version 1.00 2011/2/10
     */
    
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class ReadFile {
    
        public ReadFile() {
        }
    
    
    	//READS .CSV AND CREATES A STRING[][] OUT OF THE DATA
        public String[][] createArray(String fileName){
        	 File fileInData = new File("C:\\Users\\Mike\\Desktop\\Comp Sci\\Stock\\DATA\\" + fileName + ".csv");
        	 String [][] data = new String[7][findRows(fileInData)];
        	 int r = 0;
        	 try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileInData));
    			String line = null;
    			while((line = reader.readLine()) != null){
    				StringTokenizer z = new StringTokenizer(line, ",");
    				while(z.hasMoreTokens()){
    					for(int c = 0; c < 7; c++){
    						data[c][r] = z.nextToken();
    					}
    					r++;
    				}
    			}
        	 }
        	 catch(Exception e){
        	 	System.out.println("#ReadFile# - - - This is not a file");
        	 }
    		return data;
        }
    	//CHECKS THE FILE TO SEE IF IT EXISTS
        public boolean checkIsFile(File fileIn){
        	return fileIn.isFile();
        }
    	//FINDS THE TOTAL ROWS IN THE FILE
    	//USED FOR CREATING AN ARRAY OF THE APPROPIATE SIZE
       public int findRows(File fileIn){
       	int row = 0;
       	if(checkIsFile(fileIn)){
       		try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileIn));
    			while((reader.readLine()) != null){
    				row++;
    			}
       		}
       		catch(Exception e){
       			System.out.println("#ReadFile# - - - " + e);
       		}
       	}
       	else
       		System.out.println("#ReadFile# - - - This is not a file");
    
       	return row;
       }
    	//READS .TXT TO CREATE STRING[][] OF ALL THE STOCKS THAT ARE BEING ANALYZED
       public String[][] getStocks(String fileNameStocks){
        	 File fileInStocks = new File("C:\\Users\\Mike\\Desktop\\Comp Sci\\Stock\\DATA\\" + fileNameStocks + ".txt");
        	 String[][] stocks = new String[2][findRows(fileInStocks)];
        	 int r = 0;
        	 try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileInStocks));
    			String line = null;
    			while((line = reader.readLine()) != null){
    				StringTokenizer z = new StringTokenizer(line, ",");
    				while(z.hasMoreTokens()){
    					for(int c = 0; c < 2; c++){
    						stocks[c][r] = z.nextToken();
    					}
    					r++;
    				}
    			}
        	 }
        	 catch(Exception e){
        	 	System.out.println("#ReadFile# - - - This is not a file");
        	 }
    		return stocks;
        }
    }
    Thanks,
    Mike

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I am always getting the "#ReadFile# - - - This is not a file" error

    The program writes this message in response to any exception in the try block: and that covers a wide range of possibilities.

    To narrow things down a bit try printing a bit more information

    Java Code:
    catch(Exception e){
        System.out.println("#ReadFile# - - - This is not a file");
        System.out.println("fileInStocks=" + fileInStocks.getAbsolutePath());
        e.printStackTrace();
    }

  3. #3
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Here is the output
    #ReadFile# - - - This is not a file
    #ReadFile# - - - This is not a file
    fileInStocks=C:\Users\Mike\Desktop\Comp Sci\Stock\DATA\StockArray.txt
    java.io.FileNotFoundException: C:\Users\Mike\Desktop\Comp Sci\Stock\DATA\StockArray.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileReader.<init>(FileReader.java:55)
    at ReadFile.getStocks(ReadFile.java:74)
    at testStock.main(testStock.java:25)
    Here is the file:



    Thanks,
    Mike

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What does the command line report when you "dir" that folder? Sometimes GUI file browsers can be a little too keen to be "helpful".

  5. #5
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    I am not totally sure what you mean, but the file path is correct because i can access the dow.csv file in that folder with the ReadFile class. I just use the createArray method instead. I have a feeling there is a really stupid mistake somewhere that i am overlooking.

    Here is my test class
    Java Code:
    public static void main(String args[]){
            Stock s1 = new Stock("Dow Jones", "DOW");
            ReadFile rf = new ReadFile();
    	String[][] stocks = rf.getStocks("StockArray");
    		for(int x = 0; x<stocks[0].length;x++)
    			for(int y = 0; y<stocks.length; y++)
    				System.out.print(stocks[y][x]);

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I see that the getStocks method calls the findRows method which also reads the file. Yet the findRows method does not appear in the stack trace. Can you identify the exact line which is throwing the exception.

  7. #7
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    These are throwing the error:

    Java Code:
    /**
     * @(#)ReadFile.java
     *
     *
     * @Mike Genovese
     * @version 1.00 2011/2/10
     */
    
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class ReadFile {
    
        public ReadFile() {
        }
    
    
    	//READS .CSV AND CREATES A STRING[][] OUT OF THE DATA
        public String[][] createArray(String fileName){
        	 File fileInData = new File("C:\\Users\\Mike\\Desktop\\Comp Sci\\Stock\\DATA\\" + fileName + ".csv");
        	 String [][] data = new String[7][findRows(fileInData)];
        	 int r = 0;
        	 try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileInData));
    			String line = null;
    			while((line = reader.readLine()) != null){
    				StringTokenizer z = new StringTokenizer(line, ",");
    				while(z.hasMoreTokens()){
    					for(int c = 0; c < 7; c++){
    						data[c][r] = z.nextToken();
    					}
    					r++;
    				}
    			}
        	 }
        	 catch(Exception e){
        	 	System.out.println("#ReadFile# - - - This is not a file");
        	 }
    		return data;
        }
    	//CHECKS THE FILE TO SEE IF IT EXISTS
        public boolean checkIsFile(File fileIn){
        	return fileIn.isFile();
        }
    	//FINDS THE TOTAL ROWS IN THE FILE
    	//USED FOR CREATING AN ARRAY OF THE APPROPIATE SIZE
       public int findRows(File fileIn){
       	int row = 0;
       	if(checkIsFile(fileIn)){
       		try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileIn));
    			while((reader.readLine()) != null){
    				row++;
    			}
       		}
       		catch(Exception e){
        		System.out.println("#ReadFile# - - - This is not a file");
        		System.out.println("fileIn=" + fileIn.getAbsolutePath());
        		e.printStackTrace();
    		}
       	}
    
       	[B][COLOR="Red"]else
       		System.out.println("#ReadFile# - - - This is not a file");[/COLOR][/B]
    
       	return row;
       }
    	//READS .TXT TO CREATE STRING[][] OF ALL THE STOCKS THAT ARE BEING ANALYZED
       public String[][] getStocks(String fileNameStocks){
        	 File fileInStocks = new File("C:\\Users\\Mike\\Desktop\\Comp Sci\\Stock\\DATA\\" + fileNameStocks + ".txt");
        	 String[][] stocks = new String[2][findRows(fileInStocks)];
        	 int r = 0;
        	 try{
    			BufferedReader reader = new BufferedReader(new FileReader(fileInStocks));
    			String line = null;
    			while((line = reader.readLine()) != null){
    				StringTokenizer z = new StringTokenizer(line, ",");
    				while(z.hasMoreTokens()){
    					for(int c = 0; c < 2; c++){
    						stocks[c][r] = z.nextToken();
    					}
    					r++;
    				}
    			}
        	 }
        	 	[B][COLOR="Red"]catch(Exception e){
        		System.out.println("#ReadFile# - - - This is not a file");
        		System.out.println("fileInStocks=" + fileInStocks.getAbsolutePath());
        		e.printStackTrace();[/COLOR][/B]
    }
    
    		return stocks;
        }
    }

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I am not totally sure what you mean, but the file path is correct because i can access the dow.csv file in that folder with the ReadFile class.

    And I note that the dow.csv file is displayed in the browser as DOW. (without the extension). Now the other file is displayed as StockArray.txt and I'm wondering what the real name of that file is.

    What I mean is that it might be instructive to open a cmd prompt, navigate to that folder and use the command

    Java Code:
    dir

    to obtain a listing of the folder's contents.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It's curious to me that DOW.csv doesn't show the .csv extension, but StockArray.txt does show the .txt extension in the Windows Explorer view. My guess is that StockArray.txt is really StockArray.txt.txt. This is why pbrockway2 wanted to see the output of a DIR command.

    -Gary-

    EDIT: I hadn't seen pbrockway2's last post when I wrote this. He's saying essentially the same thing I am.
    Last edited by gcalvin; 02-11-2011 at 04:56 AM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ahh

    You only print the stack trace in the getStocks method.

  11. #11
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Alright Problem Solved!!!!

    It was .txt.txt Thanks so much guys.

    Mike

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I'm glad you've got that sorted out. SOmewhere under "Tools->Folder options..." you can turn off its hiding of file extensions. This actually effects the behaviour of some microsoft applications that will take it upon themselves to actually add bits to the filename that you type. (with the resulting confusion you've observed)

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    By the way, big blocks of code that look alike like that is a "code smell".

    Code smell - Wikipedia, the free encyclopedia

    You'll want to do some refactoring there. Also, consider using an ArrayList<String> instead of a String[] array. For one thing, you won't need to read the file twice just to see how many array elements you need. Also, StringTokenizer is not quite deprecated, but "its use is discouraged in new code".

    StringTokenizer (Java Platform SE 6)

    -Gary-

  14. #14
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks calvin,

    your right some reorganization is in order, but before i get started on that, what should I use to replace tokenizer?

    Mike

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    String.split is the preferred method.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    <wagsfinger /> ;)

    Did you read the link gcalvin posted? (The one that describes StringTokeniser)

  17. #17
    mgeno216 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Ya i did, he posted a link to the string tokenizer page in the api. How does that help me when he said that string tokenizer was not the prefered method. Am i missing something?

    Thanks junky i will look into it

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Am i missing something?
    I hope you appreciate that there was no heat in my comment... But, yes, you missed this:

    "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

Similar Threads

  1. Need help wit reading txt file into input.
    By ShinTec in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:40 PM
  2. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 12:41 PM
  3. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 05:02 PM
  4. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 10:07 PM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Posting Permissions

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