Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

reding in csv file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic