Got something to write about? Check out our Article Builder.

Java Beginners

Moderators: zibadian
Number of threads: 889
Number of posts: 1946

This Forum Only
Post New Thread

Report
cannot read the files from a directory Posted by hamyfox on 3 Nov 2009 at 5:10 PM

Hi,

I am new to java. I am trying to read and clean several text files from a directory than the working one .
I am using relative path to do that. I have a mac and I am using command window. I have absolutely no
idea why the program is not working. Below is the program and output. I would appreciate
any input. Thanks.

/*
* mclean3.java
*/

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


public class mclean3 {

public static void main(String args[]) {
if(args.length == 0) {
System.err.println("usage: java Type directory name ");
} else {
String path = args[0];
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
String files;
String wfile;
BufferedReader br = null;
String line = null;
String pattern = "[^A-Za-z]";

for(int i = 0; i < listOfFiles.length; i++) {
files=listOfFiles[i].getName();

System.out.println( System.getProperty( "user.dir" ) );
System.out.println(path);

wfile = "\""+path+"/"+files+"\"";
try {
System.out.println(files);
System.out.println(wfile);
br = new BufferedReader(new FileReader(wfile));
line = br.readLine();
while(line != null) {
//line = line.replaceAll(pattern, " ");
System.out.println(line);
//line = br.readLine();
}
br.close();
} catch(FileNotFoundException exc1) {
System.err.println(" File "+wfile+" not found");
} catch(IOException exc2) {
System.err.println("Input/Output error with the file "+wfile);
}
}
}
}

} // end of class mclean3
hamy-temkits-macbook-pro:javatutorial mtemkit$ emacs
hamy-temkits-macbook-pro:javatutorial mtemkit$ javac mclean3.java
hamy-temkits-macbook-pro:javatutorial mtemkit$ java mclean3 ~/Desktop/java/javatutorial/hfiles
/Users/mtemkit/Desktop/java/javatutorial
/Users/mtemkit/Desktop/java/javatutorial/hfiles
.DS_Store
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/.DS_Store"
File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/.DS_Store" not found
/Users/mtemkit/Desktop/java/javatutorial
/Users/mtemkit/Desktop/java/javatutorial/hfiles
1911.txt
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/1911.txt"
File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/1911.txt" not found
/Users/mtemkit/Desktop/java/javatutorial
/Users/mtemkit/Desktop/java/javatutorial/hfiles
hello.txt
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/hello.txt"
File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/hello.txt" not found
/Users/mtemkit/Desktop/java/javatutorial
/Users/mtemkit/Desktop/java/javatutorial/hfiles
twice1911.txt
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/twice1911.txt"
File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/twice1911.txt" not found

Report
Re: cannot read the files from a directory Posted by sugi27 on 6 Nov 2009 at 4:13 AM
hi..
What do want to do actually.
DO you need to list all the files in the current working directory and clear the contents of it?...

just check this ..it mite help u.
This will actually accept the input(path from which the files have to be listed and cleared) from the user,list all the files and folders in it,check if it is a file/folder,if it is a file, clear the contents of it.
It works only for one level of folders and files.I am not sure of the problem, you are facing.just check out this

String filename;
String pattern="[^A-Za-z]";
System.out.println("enter the name of the folder to be searhed for files");
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
filename=br.readLine();
System.out.println("path entered is"+filename);
File f =new File(filename);
File[] files=f.listFiles();
for(int i=0;i<files.length;i++)
{
System.out.println(files[i].getAbsolutePath());
if(files[i].isFile())
{
System.out.println("is a file");
String filecontent;
FileReader fr=new FileReader(files[i]);
BufferedReader br1=new BufferedReader(fr);
while((filecontent=br1.readLine())!=null)
{

//filecontent=filecontent.replaceAll(pattern,"");
FileWriter fw=new FileWriter(files[i]);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(" ");

}
}
}





 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.