Results 1 to 4 of 4
  1. #1
    TheWolfe is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Simple Java IO Questions

    In my class, we are learning about Java IO right now, using Eclipse. I am currently trying to complete a project that uses Java IO extensively. I am having trouble with the syntax. Help would be appreciated :D

    Here is my attempt at a very simple program that creates a file, and writes inside it "Hello World" :

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class CreateFile
    {
        public static void main(String[] args)
        {
        		try{
        			Scanner in = new Scanner(System.in);
    		    	System.out.print("Enter a filename: ");
    		    	String filename = in.nextLine();
    		    	FileWriter outFile = new FileWriter(filename);
    		    
    		    	 	outFile.write("Hello World!");
    		    	    outFile.close();
    		    	}	
    	    	catch(IOException e)
    	    	{
    	    		System.out.println("Could not open file.");
    	    	}
        }
    }
    It seems to work when I run it, but I'm having trouble locating the text file that I create. Where does this file end up?

    And more importantly, when I want my program to read from a file, where do I need to put this file on my computer?

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    If you just input the filename then it will be saved on you class path.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't think that it's saved directly on the class path (where the class files are loaded from), and indeed a classpath can contain many directories but rather in the user directory. To the OP, put this in the program above:

    Java Code:
    System.out.println(System.getProperty("user.dir"));

  4. #4
    grdnalbert is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Simple and easy java IO recall program

    So I have spent way to much time scowering the web on were to find a way to recall .txt files. Here is how I did it.
    I first made one class
    import java.util.*;
    import java.io.*;
    public class filetest {

    /**
    * @param args
    */
    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.println("File name: ");
    try
    {
    String filename=input.next();
    System.out.println(filename);
    System.out.println();
    showfile.showFile(filename);
    }
    catch(FileNotFoundException e)
    {
    System.out.println(e);
    }
    catch(IOException e)
    {
    System.out.println(e);
    }
    // TODO Auto-generated method stub

    }

    }

    now in order for this class to work you must use a second class which the code is:

    import java.io.*;
    public class showfile {
    public static void showFile(String filename) throws IOException,
    FileNotFoundException{
    int c;
    File input=new File(filename);
    FileInputStream in=new FileInputStream(input);
    while((c=in.read())!=-1)
    {
    System.out.print((char)c);
    }
    in.close();
    }
    }

    once all of this is in place go back to your first class and in order to find your file you must type the directories it is under.

    EXAMPLE:
    Input Filename:
    C:\test.txt
    test.txt
    hello world

    hope that helps!

Similar Threads

  1. JAVA questions
    By melody in forum New To Java
    Replies: 0
    Last Post: 10-28-2009, 11:07 PM
  2. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 05:57 AM
  3. Few Questions about java
    By Grom in forum NetBeans
    Replies: 11
    Last Post: 09-13-2008, 02:26 PM
  4. 2 simple java questions
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 09:35 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
  •