Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Creating a File with eclipse:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Creating a File with eclipse:

    import java.io.*; 
     
    public class ReadNumbers
    {
    void input()
    {
    	String File_name = "num.txt";
    	File numbers = new File(File_name);
     
    	if(numbers.exists()){System.out.println("The file was found");} 
    	else {System.out.println("The file was not found!!!");}
    }
    	public static void main(String[] args) 
    	{
    		ReadNumbers read = new ReadNumbers();
     
    		read.input();
    	}
     
    }

    I am using eclipse and I went to file, add new file and named it num.txt:shot.png like show in the picture.

    However it cannot find it. What am I doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    The existence of a File object in a java program has nothing to do with the existence of a file on the disk.
    The File class has methods that can be used to create a file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    void input()
    {
    	String File_name = "num.txt";
    	File numbers = new File(File_name);
     
    	if(numbers.exists()){System.out.println("The file was found");} 
    	else
    	{
    		final Formatter make_file;
    		try{make_file = new Formatter(File_name);} 
     
    		catch(Exception e)
    		{ 
    		System.out.println("File was not found but we created one for you.");
    		}
     
    	}
    }

    This works I just have to figure out where it is making the file and more importantly, how to read the files input.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    You will have to write something into the file so there will be something to read.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    import java.io.*; 
    import java.util.*;
    //import java.lang.*;
     
    public class ReadNumbers
    {
    private Formatter make_file;
    private String File_name = "num.txt";
     
    void input()
    {
    	File numbers = new File(File_name);
     
    	if(numbers.exists()){System.out.println("The file was found");} 
    	else
    	{
    		try{make_file = new Formatter(File_name);} 
     
    		catch(Exception e){ System.out.println("You have an error.");}	
    	}
    }
    void WriteFile()
    {
    	try
    	{
    		FileOutputStream f = new FileOutputStream(File_name);
     
    		for(int i=0;i<10;i++)
    		{
    			f.write(i);
    		}
    		f.close();
    	}
    	catch(IOException e)
    	{
    		System.out.println("There was an error");
    	}
     
    }
     
     
    	public static void main(String[] args) 
    	{
    		ReadNumbers read = new ReadNumbers();
     
    		read.input();
    		read.WriteFile();
    	}
     
    }

    This is my first project messing with files in Java as you might be able to tell. However now that I can run this program there is nothing written to it. It just deletes whatever I had in the file previously.

    --- Update ---

    void WriteFile()
    {
    	try
    	{
    		FileOutputStream f = new FileOutputStream(File_name);
    		String name = "Write this to the file...";
     
    		byte b[] = name.getBytes();
     
    		for(int i=0;i<b.length;i++)
    		{
    			f.write(b[i]);
    		}
    		f.close();
    	}
    	catch(IOException e)
    	{
    		System.out.println("There was an error");
    	}
     
    }

    Now I got a string to work but not the integers that I posted above?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    but not the integers
    Was anything written to the file? Writing an int value to a file does not write a String. An int takes 4 bytes and will have binary values that often are not readable in a text editor. You would need a hex editor to read a file containing int values and see its contents as hex values.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    This is what is bugging me here and I thinking I am over thinking it. I am trying to find a way similar to C++ to input and output file. For example
     
    ifstream in("num.txt",ios::in);
    int a=0,b=0;
     
    while(in>>a>>b)
    {
    std::cout<<a<<""<<b<<"\n";
    }

    Something along those line. For lets say I know the structure of the file. It seems way more complicated in Java, am I correct?

    When I used the char array in the second example I had output to the file, but not with the first example.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    Yes, java does not have (yet) the magic macros that hide all the method calls with a few symbols like << or >>.
    The method calls must be explicitly made in the code by the programmer.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    void ReadFile() throws Exception 
    {
    	FileReader file = new FileReader(File_name);
    	BufferedReader reader = new BufferedReader(file);
     
    	String text ="";
    	String line = reader.readLine();
     
    	while(line != null)
    	{
    		text += line;
    		line = reader.readLine();
    	}
    	System.out.println(text);
     
    	reader.close();
    }

    How is this? Is there a better way? What if I had values like (1 8 name 45) and I knew that was the files format. What is a good approach to that?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    The Scanner class has a family of methods that can be used to read different data types from a file.
    Or if the records are read into Strings as the posted code does it,
    the String can be passed to the Scanner class's constructor so that the class's methods can be used
    or the String can be split into separate parts using the String class's split() method and then the Integer class's parse method can be used to convert the Strings (like "1" or "8") to int values.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    Thank you very much... I will continue will work on this program. I am comfortable with and decided to learn Java.
    Thanks for the help, I am sure I will be back soon for more questions. Hopefully I can contribute to this site more when I am better.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a File with eclipse:

    This is a good way to learn how stuff works. I've used it a lot myself.
    Write lots of small test programs to see how stuff works.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    jocdrew21 (April 14th, 2014)

  14. #13
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Creating a File with eclipse:

    void ReadFile() throws Exception 
    {
    	FileReader file = new FileReader(File_name);
    	BufferedReader reader = new BufferedReader(file);
     
    	String text ="";
    	String line = reader.readLine();
     
    	while(line != null)
    	{
    		text += line; //text is assigned the string value that was in the buffer
    		line = reader.readLine();//reads next line
    	}
    	System.out.println(text);//once end of file was reached the String is printed
     
    	reader.close();
    }

    I am reading Java the complete Reference Eighth Edition and it is rather frustrating how many ways there are to handle files. The above example seems to be pretty simple, is this the most common way? What is your favorite and why?

    Lets say I wanted to break down the input because I new the file format, is DataInputStream the simplest and efficient way to handle that? I know you already told me about one way to handle this but since there were some many ways to handle files I figured there was something designed for such a thing.

    try(DataInputStream din=new DataInputStream(new FileInputStream("num.txt")))
    {
    int age = read.readInt();
    String DOB = read.readString();
    String name = read.readString();
     
    //down here I could make a list of nodes with the above data and push in the information to use it later
    }
    catch(FileNotFoundException e)
    {
    System.out.println("File was not found");
    return;
    }

    Which leads me to my next question:

     
    struct foo
    {
    int age;
    std::String DOB;
    std::String Name;
    }MyFoo
     
    std::list<MyFoo>Mylist;  //here I could call push after I got all the data from the file like I explained above
    int main()
    {
    //some stuff
    }

    What is a good way to handle this in Java? Java doesn't have structs but it does have inner classes. Is that what I need here?

Similar Threads

  1. Creating weblogic server in Eclipse IDE
    By Javacrazy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 24th, 2013, 12:58 PM
  2. Creating folders in Eclipse
    By felixb in forum Java IDEs
    Replies: 3
    Last Post: October 5th, 2013, 03:45 AM
  3. Creating JAR file in eclipse HELP !
    By Maheshkh in forum Java IDEs
    Replies: 3
    Last Post: September 16th, 2011, 12:37 PM
  4. Replies: 5
    Last Post: November 13th, 2010, 01:53 PM