• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing files, detemining if they exist...  RSS feed

 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to determine at the start of my program if a file exists. In the File API there is a method, exists() that returns a boolean value if said file exists. In order to access the exists() method, you have to initialize the File object, so effectively you have to create a File to see if it exists.

For example, to see if alpha file exists would you have to...

File alpha = new File("alpha");
S.O.P.(alpha.exists());

...to see if the file exists using this method, when it obviously does because you have to make it to check!

Any suggestions on how to check if a file exists without doing this?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File class is just an abstract representation of a file name. It does not actually represent a file on your computer. so File.exists() will do exactly what you want.
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so how would you create an actual file?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.createNewFile() possibly ? Or if you want to write to the file as well, look at the writer and stream classes such as FileWriter and FileOutputStream.
 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A file wont be created on your disk until you actually write some streams on the File object.
So unless you have a specific requirement, you dont really need to check if the file exisits or not ..because it always creates a new file or override the old one in Java.. Am I right on this?

Regards,
Jiafan
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have to write a mapping of newsgroups and articles read onto the file (newsrc).
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Walker:
Well I have to write a mapping of newsgroups and articles read onto the file (newsrc).


As has been mentioned, createNewFile() will create the file if it doesn't already exist and is an atomic operation. you should look at the classes in java.io for writing to the file. Whether or not a given class will create the file if it doesn't exist, overwrite the file if it exists, or append to it is something you should consult the documentation for informaton on. FileOutputStream and FileWriter both provide constructors that accept the File and a boolean argument specifying whether to append to the file or overwrite it. In my experience with the java.io API most implementations are going to create the file if they can and overwrite the file if it exists unless you specify otherwise.

Once again, this is in regards to using an OutputStream or Writer of some sort. The File class merely represents a File path in the abstract, creating one does not create a new file on disk, passing that abstract representation to another class that will create it is another story.
[ September 15, 2006: Message edited by: Ken Blair ]
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I'm trying to read in newsgroups from the host newsserver listed in my instance data. The programs compiles but freezes at startup. Here's the code (I guess copy and paste it into an editor to view it easier)


import java.io.*;
import java.net.*;
import java.util.*;



public class NewsManager {
public static void main(String[] args) {
//instance constants


//instance variables
String fname = ".newsrc"; //name of file to read from
String host = "news.readfreenews.net";
String home;//user's home directory
String separator;//path separator
int nntpServicePortNumber = 119;

/*********************************************************************/

//get user home directory and file separator
home = System.getProperty("user.home");
File dir = new File(home);
separator = dir.pathSeparator;

//check if .newsrc file exists, if not create it
File newsrc = new File(dir, ".newsrc");
try {
newsrc.createNewFile(); }
catch (IOException ex) {
System.out.println("File IO error"); }

BufferedReader in = null;
PrintWriter out = null;
Socket nntpSocket = null;

try {
FileWriter fw = new FileWriter(newsrc, true);
BufferedWriter bw = new BufferedWriter(fw);

try {
nntpSocket = new Socket(host, nntpServicePortNumber);
out = new PrintWriter(nntpSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
nntpSocket.getInputStream())); }

catch (UnknownHostException e) {
System.err.println("Don't know about host " + host);
System.exit(1); }

catch (IOException e) {
System.err.println("Couldn't get I/O for the connection.");
System.exit(1); }

try {
String response200 = in.readLine();

if (!response200.startsWith("2")) {
System.err.println("Protocol Error");
System.exit(1); }

String response215 = in.readLine();

if (!response215.startsWith("215")) {
System.err.println("Protocol Error");
System.exit(1); }

for (; {
String line = in.readLine();
if (line.startsWith(".")) break;
StringTokenizer toks = new StringTokenizer(line);
String name = toks.nextToken();

int first = 0;
String firstAsString = toks.nextToken();
try {
first = Integer.parseInt(firstAsString); }

catch (NumberFormatException ex) {
System.err.println("Bad number format? " + firstAsString);
System.exit(1); }

int last = 0;
String lastAsString = toks.nextToken();
try {
last = Integer.parseInt(lastAsString); }

catch (NumberFormatException ex) {
System.err.println("Bad number format? " + lastAsString);
System.exit(1); }

NewsGroup n = new NewsGroup(name, first, last);

try {
bw.write(name + first + last); }
catch (IOException ex) {
System.out.println("File IO error"); }

System.out.println(n);
}//end for

out.close();
in.close();
nntpSocket.close();
bw.close();
}//end try

catch (IOException ex) {
System.err.println("IO failure.");
ex.printStackTrace(); }
}//end try

catch (IOException ex) {
System.out.println("File IO error"); }
}
}
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has something to do with this portion of code...

String response215 = in.readLine();

if (!response215.startsWith("215")) {
System.err.println("Protocol Error");
System.exit(1); }
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your for loop just an infinite loop with just two semicolons?

It's hard to tell because of the formatting, but is there a place where you try to break out of the loop?
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It never even gets into the for loop, it stalls at the portion of code for some reason....
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code you posted is right before the for loop. You can try adding several SOP lines at the beginning of the loop and before it to see where the code stops.
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that's what I did. That portion I posted is where it stops.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be the second call to readLine()? Do you just want to read the response once and see if it starts with 200 or 215?
 
Greg Walker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something to do with the nntp protocol...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Walker:
Here's the code (I guess copy and paste it into an editor to view it easier)


Or you could use code tags when posting. Highlight your code after pasting it into your reply and then click the 'code' button below.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!