• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Directory  RSS feed

 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that reads a text file and then formats the data and puts it out and this works great, but i need to know if there is a way to read all of the text files in a folder so i can run the program once.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the java.io.File class to get a list of all the files and loop through them.
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
Thanks for the info, got me off to a great start. I am having trouble through with the output file. I get the follwoing error when compiling.
:\JavaPrograms\FileDirectory.java:122: cannot resolve symbol
symbol : variable out
location: class FileDirectory
out.print(Copybook);

import java.io.*;

public class FileDirectory
{
public static void main(String args[]) throws IOException
{
File dir = new File("C:\\tmp1");
String filedir = "C:\\tmp1\\";
String filep = null;
if ((dir.exists()) && (dir.isDirectory()))
{
File datao = new File("example.txt");
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(datao)));
System.out.println("Directory: " + dir.getCanonicalPath());
String[] files = dir.list();

for (int i = 0; i < files.length; i++)
{
filep = filedir + files[i];
System.out.println(filep);
processCopybook(filep);
}
out.close();
}
else
{
System.out.println("directory does not exists");
}

}
private static void processCopybook(String filen) throws IOException
{
---some code then
out.print(Copybook);

I want only one output file from all of the input files.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't access a variable declared in one block of code in another block of code. That's what we call the scope of the variable. You either have to change the scope so it can be accessed by both blocks, or pass it as an argument to processCopybook.
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After rearanging my code i get the following error .
C:\JavaPrograms\FileDirectory.java:3: '{' expected
public class FileDirectory throws IOException

import java.io.*;

public class FileDirectory throws IOException
{ File datao = new File("example.txt");
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(datao)));
public void main(String args[]) throws IOException
{

File dir = new File("C:\\tmp1");
String filedir = "C:\\tmp1\\";
String filep = null;
if ((dir.exists()) && (dir.isDirectory()))
{

System.out.println("Directory: " + dir.getCanonicalPath());
String[] files = dir.list();

for (int i = 0; i < files.length; i++)
{
filep = filedir + files[i];
System.out.println(filep);
processCopybook(filep);
}
out.close();
}
else
{
System.out.println("directory does not exists");
}

}
private void processCopybook(String filen) throws IOException
{ some code
out.print(Copybook);
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two problems:

1) You have declared your class to throw an exception. Only methods can throw exceptions.

2) Related to this, you have put your code directory into the class. The code should be inside a method, such a public static void main(String[] args).
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the way i had it orignally but then can't use the variable out in the processCopybook method. How can i define the variable out in the class to be able to use it at all levels?
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you had the right idea. But instead of moving all of your code out of the method and into class scope, just move the declaration of the 'out' variable. This will make out visible to all methods in the class.

So you can change the start of your class to look like this:

Also note that I removed the declaration of 'out' from your main method as well. Otherwise you will declare a second variable called out (but with a different scope) and all sorts of Bad Things will happen.
[ August 18, 2005: Message edited by: Stuart Gray ]
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now getting a NullPointerException on the out.print(Copybook); statement.
[ August 18, 2005: Message edited by: r phipps ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going back to what Stuart wrote: do you now have one or two declarations of "out"? I suspect you may have two, which will cause all kinds of problems, as Stuart mentions.
[ August 19, 2005: Message edited by: Ulf Dittmer ]
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALso, make sure you are removing only the declaration, not the assignment. The line inside your if statement should look like this:

The initial 'PrintWriter' (i.e. the declaration) is removed.
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank everybody,
I had figured that out when i got ready to post my reply, stuart had posted the same thing. I apreciate everybody's help and for the direction and not just spoon feeding me a solution. This is my first real meaningful Java Program and has been a real learning experience. Just have some more parsing logic to do, but i think the hard part is over with. Again thanks for all the help.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I apreciate everybody's help and for the direction and not just spoon feeding me a solution.

You're welcome. Although sometimes it might look like we only give a little bit of the solution so we can enjoy watching you suffer, I assure you that is not the case
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think sometimes it is a Little bit of both
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!