1 Replies - 294 Views - Last Post: 21 September 2011 - 06:32 PM Rate Topic: -----

#1 mistermeaner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 11

File IO issue

Posted 21 September 2011 - 01:24 PM

Hey I was having loads of problems with my FileIO project. The text below is that which i typed but I then ran it again (hadnt moved any files or anything) and all of a sudden it sprang to life in the terminal! Can anyone explain this? The question still remains as to why netbeans cannot find the file when using windows yet the directories/placements of the data.txt file are exactly the same!? Please aid my sanity!

Thanks in advance :)





Hey guys, Im having some trouble with a simple file IO program, its supposed to just read the file then print the results to the output. I think that its more a problem of me not placing the data.txt file in the correct directory.

For example in windows i get the file not found error even though its technically in the same location as when i run it in ubuntu (Just within the FileIO project file with all of the src and dist directories). But for some reason when i run main in ubuntu it works perfectly and can find data.txt :S However when i build a .java in ubuntu (and copy the same data.txt into the same directory as the .java) it wont run and states file not found?

Any help is greatly appreciated! and heres the code and directory trees :)


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fileio;

import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;

/**
 *
 * @author sredman
 */
public class Main
{

    public static void main(String[] args) throws IOException
    {

        System.out.println(new File(".").getAbsolutePath());

        Scanner inputFile;
        inputFile = new Scanner(new File("data.txt"));

        DecimalFormat myformat = new DecimalFormat("0.##");

        String name;
        float price;

        price = inputFile.nextFloat();
        inputFile.nextLine();

        while (price != 0)
        {
        name = inputFile.nextLine();
        System.out.println(name + " -- " + myformat.format(price));

        price = inputFile.nextFloat();
        inputFile.nextLine();
        }
        
    }

}





|-FileIO
|---build
|-----classes
|-------fileio
|-----empty
|---dist
|---nbproject
|-----private
|---src
|-----fileio
|---test






Is This A Good Question/Topic? 0
  • +

Replies To: File IO issue

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: File IO issue

Posted 21 September 2011 - 06:32 PM

I don't know where you have your source folder, but the base folder of your project is the default path where java will look for your text file. So let's say that your data.txt is located in C:\myproject\src\fileio\data.txt. Here the base folder of the project is C:\myproject so there is where java will look for data.txt at least that you specify the absolute path. Now the question is where do you have the file data.txt ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1