Results 1 to 5 of 5
  1. #1
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default I'm having trouble with inputstreams.

    I'm not sure if the program i'm running eclipse is doing this but. I'm not able to do inputStreams like I've been trying to. I'm trying to run a simple program. I'm not very fond with ecplise. I'm not sure if it is the code or the program.
    I come up with these errors.

    FileInputStream.open(String)line: not available[native method]
    FileInputStream.<init>(File)line: not available
    Scanner.<init>(File)line: not available
    prog201.main(String[])line: 10

    Then I get a window popping up asking me for the source.
    I go to the source and it still doesn't work.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    class prog201
    {
      public static void main (String[] args) throws IOException
      { 
        File    file = new File("myData.txt");   // create a File object
        Scanner scan = new Scanner( file );      // connect a Scanner to the file
        int num, square;
    
        num = scan.nextInt();
        square = num * num ;   
    
        System.out.println("The square of " + num + " is " + square);
      }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: I'm having trouble with inputstreams.

    Your IDE is giving you its own error messages.
    What happens if you execute the program with the java command?

  3. #3
    bams is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: I'm having trouble with inputstreams.

    In the top of the class you have to import packages

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    Hope this helps.. Instead of all in one method. You can refactor code to separate methods when creating, reading and writing file.

    Java Code:
    public static void main(String args[]) {  
    		try { 
    			// create a File object
    			File file = new File(DATA_FILE_NAME);
    			boolean exist = file.createNewFile();			
    			if (!exist) {
    				System.out.println("File already exists."); 
    			} else {
    				System.out.println("File created successfully.");
    			}
    			
    			// Write to file.
    			FileWriter fstream = new FileWriter(DATA_FILE_NAME);
    			BufferedWriter out = new BufferedWriter(fstream);
    			try{
    				System.out.println("Please enter a number :");
    				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    				out.write(in.readLine());
    			}finally{
    				if(out != null){
    					out.close();	
    				}
    			}
    			
    			// Read from File.
    			// connect a Scanner to the file 
    			Scanner scanner = new Scanner(new FileReader(DATA_FILE_NAME)); 
    			int num = scanner.nextInt(); 
    			int square = num * num;
    			System.out.println("The square of " + num + " is " + square);
    		} catch (FileNotFoundException e) {
    			System.err.println("File Not Found Exception: "+e.getMessage()); 
    			e.printStackTrace();
    		} catch (IOException e) { 
    			System.err.println("Input/ Output Exception: "+e.getMessage());		
    			e.printStackTrace();
    		} 
    	}
    Last edited by bams; 02-02-2012 at 06:59 AM.

  4. #4
    ShaolinMunky is offline Computer Science Student
    Join Date
    Feb 2012
    Location
    Savannah, GA
    Posts
    4
    Rep Power
    0

    Default Re: I'm having trouble with inputstreams.

    Is myData.txt in the same directory as your .class file? is your IDE set to a default source location, or did you specify a directory? When my IDE gives me trouble, I copy the file into notepad (remove packaging code) and save it in a directory that's easy to get to from the command prompt. Then I save my file in the same directory and compile and run from the command prompt. Usually that solves my problems.

    --SM

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: I'm having trouble with inputstreams.

    Quote Originally Posted by Zeronitic View Post
    I come up with these errors.

    FileInputStream.open(String)line: not available[native method]
    FileInputStream.<init>(File)line: not available
    Scanner.<init>(File)line: not available
    prog201.main(String[])line: 10
    That's not an error.
    That's the tail of a stacktrace.
    The exception itself is in the line before this.

    Without the exception then we don't know exactly what the problem is...please post the exception text as well as the stack trace.

Similar Threads

  1. Io trouble need help
    By loopsnhoops in forum New To Java
    Replies: 18
    Last Post: 06-03-2011, 04:35 AM
  2. Im having trouble with.......
    By Java Noobie in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 06:06 PM
  3. GUI Trouble
    By rvgsd in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 01:10 AM
  4. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 04:18 PM
  5. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 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
  •