This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reding in csv file  RSS feed

 
Jill Nilsson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to read in a csv file, but I get an error: the file does not exist.
I am not sure that I fully understand how the path and location works.
Cam anyone give my a hand here?

import java.io.*;

public class TryFile {

public static void main(String[] args) {

//create an object that is a directory
File myDir = new File("C:/eclipse/workspace/blueBug/src");
System.out.println(myDir.getAbsolutePath() + (myDir.isDirectory()?" is ":" is not ") + "a directory");

//create an object that is a file
File myFile = new File(myDir, "forcastFile.csv");


System.out.println(myFile + (myFile.exists()?" does":" does not ") + "exist");
System.out.println("You can" + (myFile.canRead()?" ":"not ") + "read " + myFile);

System.out.println("You can" + (myFile.canWrite()?" ":"not ") + "write " + myFile);


return;
}
}

////////////////////////
Result:
C:\eclipse\workspace\blueBug\src is a directory
C:\eclipse\workspace\blueBug\src\forcastFile.csv doesexist
You can read C:\eclipse\workspace\blueBug\src\forcastFile.csv
You can write C:\eclipse\workspace\blueBug\src\forcastFile.csv

Thanks,
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are asking... you output says that the file exist.

Henry
 
Jill Nilsson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My appologize, for not being more clear.
Questions:
1.Where am I suppose to place the file? As far as the path and location is concerened?
2. Once it it is working, how do I actually read out the records to the screen/console or a new file.

I am thinking that I should use :
while next().....//as long as there are reords in the file.

Can anyone give some directions?
Thanks again
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jill Nilsson:
1.Where am I suppose to place the file? As far as the path and location is concerened?
2. Once it it is working, how do I actually read out the records to the screen/console or a new file.


1. You place the file where you want the file. It's your program, you get to decide...
2. The File class is for manipulation of the file/directory in the file system. It is not for opening the file to read/write. To read from the file, you can use either the FileInputStream or FileReader class.

Henry
 
Jill Nilsson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while trying to figure this out....
I found a sample on the java.sun.com
I set this up just as i did the previous exaple that I posted. But I get a "file not found exception"
I just don't get the difference....
Could anyone give me a hint...?

import java.io.*;

public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)
out.write(c);

in.close();
out.close();
}
}

Thanks,
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just ran the "Copy" program... it works fine...

Maybe you have the source file in the wrong place. Add the following line of code in the program to see exactly what file you are trying to open.

System.out.println(inputFile.getAbsoluteFile());

Henry
 
Jill Nilsson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

I did have the source folder in the "src" folder, which was one step to far.
I needed to place the file in the project folder,
Thanks again for your help, I really do appreciate your time
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!