<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'cannot read the files from a directory' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'cannot read the files from a directory' posted on the 'Java Beginners' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Wed, 23 May 2012 21:20:14 -0700</pubDate>
    <lastBuildDate>Wed, 23 May 2012 21:20:14 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>cannot read the files from a directory</title>
      <link>http://www.programmersheaven.com/mb/java_beginners/408788/408788/cannot-read-the-files-from-a-directory/</link>
      <description>&lt;br /&gt;
  Hi,&lt;br /&gt;
&lt;br /&gt;
  I am new to java. I am trying to read and clean several text files from a directory than the working one .&lt;br /&gt;
  I am using relative path to do that. I have a mac and I am using command window. I have absolutely no&lt;br /&gt;
  idea why the program is  not working. Below is the program and output. I would appreciate&lt;br /&gt;
  any input. Thanks.&lt;br /&gt;
&lt;br /&gt;
/* &lt;br /&gt;
 * mclean3.java&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class mclean3 {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String args[]) {&lt;br /&gt;
	if(args.length == 0) {&lt;br /&gt;
	    System.err.println("usage:  java Type directory name ");&lt;br /&gt;
	} else {&lt;br /&gt;
	    String path = args[0];&lt;br /&gt;
	    File folder = new File(path);&lt;br /&gt;
	    File[] listOfFiles = folder.listFiles();&lt;br /&gt;
	    String files;&lt;br /&gt;
	    String wfile;&lt;br /&gt;
	    BufferedReader br = null;&lt;br /&gt;
            String line = null;&lt;br /&gt;
	    String pattern = "[^A-Za-z]";&lt;br /&gt;
   &lt;br /&gt;
            for(int i = 0;  i &amp;lt; listOfFiles.length;  i++) {&lt;br /&gt;
		files=listOfFiles[i].getName();&lt;br /&gt;
&lt;br /&gt;
                System.out.println( System.getProperty( "user.dir" ) );&lt;br /&gt;
		System.out.println(path);&lt;br /&gt;
       &lt;br /&gt;
		wfile = "\""+path+"/"+files+"\"";&lt;br /&gt;
		try {&lt;br /&gt;
                    System.out.println(files);&lt;br /&gt;
                    System.out.println(wfile);&lt;br /&gt;
		    br = new BufferedReader(new FileReader(wfile));&lt;br /&gt;
		    line = br.readLine();&lt;br /&gt;
		    while(line != null) {&lt;br /&gt;
			//line = line.replaceAll(pattern, " ");   &lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
			//line = br.readLine();                           &lt;br /&gt;
		                         }&lt;br /&gt;
		    br.close();&lt;br /&gt;
		}  catch(FileNotFoundException exc1) {&lt;br /&gt;
		    System.err.println(" File "+wfile+" not found");&lt;br /&gt;
		}  catch(IOException exc2) {&lt;br /&gt;
		    System.err.println("Input/Output error with the file "+wfile);&lt;br /&gt;
		}&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
   &lt;br /&gt;
}   // end of class mclean3&lt;br /&gt;
hamy-temkits-macbook-pro:javatutorial mtemkit$ emacs&lt;br /&gt;
hamy-temkits-macbook-pro:javatutorial mtemkit$ javac mclean3.java&lt;br /&gt;
hamy-temkits-macbook-pro:javatutorial mtemkit$ java mclean3 ~/Desktop/java/javatutorial/hfiles&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial/hfiles&lt;br /&gt;
.DS_Store&lt;br /&gt;
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/.
DS_Store"&lt;br /&gt;
 File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/.
DS_Store" not found&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial/hfiles&lt;br /&gt;
1911.txt&lt;br /&gt;
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/1
911.txt"&lt;br /&gt;
 File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/1
911.txt" not found&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial/hfiles&lt;br /&gt;
hello.txt&lt;br /&gt;
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/h
ello.txt"&lt;br /&gt;
 File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/h
ello.txt" not found&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial&lt;br /&gt;
/Users/mtemkit/Desktop/java/javatutorial/hfiles&lt;br /&gt;
twice1911.txt&lt;br /&gt;
"/Users/mtemkit/Desktop/java/javatutorial/hfiles/t
wice1911.txt"&lt;br /&gt;
 File "/Users/mtemkit/Desktop/java/javatutorial/hfiles/t
wice1911.txt" not found&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java_beginners/408788/408788/cannot-read-the-files-from-a-directory/</guid>
      <pubDate>Tue, 03 Nov 2009 17:10:46 -0700</pubDate>
      <category>Java Beginners</category>
    </item>
    <item>
      <title>Re: cannot read the files from a directory</title>
      <link>http://www.programmersheaven.com/mb/java_beginners/408788/408914/re-cannot-read-the-files-from-a-directory/#408914</link>
      <description>hi..&lt;br /&gt;
What do want to do actually.&lt;br /&gt;
DO you need to list all the files in the current working directory and clear the contents of it?...&lt;br /&gt;
&lt;br /&gt;
just check this ..it mite help u.&lt;br /&gt;
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.&lt;br /&gt;
It works only for one level of folders and files.I am not sure of the problem, you are facing.just check out this&lt;br /&gt;
&lt;br /&gt;
	String filename;&lt;br /&gt;
	String pattern="[^A-Za-z]";&lt;br /&gt;
	System.out.println("enter the name of the folder to be searhed for files");&lt;br /&gt;
	InputStreamReader isr=new InputStreamReader(System.in);&lt;br /&gt;
	BufferedReader br=new BufferedReader(isr);&lt;br /&gt;
	filename=br.readLine();&lt;br /&gt;
	System.out.println("path entered is"+filename);&lt;br /&gt;
	File f =new File(filename);&lt;br /&gt;
	File[] files=f.listFiles();&lt;br /&gt;
	for(int i=0;i&amp;lt;files.length;i++)&lt;br /&gt;
	{&lt;br /&gt;
		System.out.println(files[i].getAbsolutePath());&lt;br /&gt;
		if(files[i].isFile())&lt;br /&gt;
		{&lt;br /&gt;
			System.out.println("is a file");&lt;br /&gt;
			String filecontent;&lt;br /&gt;
			FileReader fr=new FileReader(files[i]);&lt;br /&gt;
			BufferedReader br1=new BufferedReader(fr);&lt;br /&gt;
					while((filecontent=br1.readLine())!=null)&lt;br /&gt;
			{&lt;br /&gt;
	&lt;br /&gt;
				//filecontent=filecontent.replaceAll(pattern,"");&lt;br /&gt;
				FileWriter fw=new FileWriter(files[i]);&lt;br /&gt;
				BufferedWriter bw=new BufferedWriter(fw);&lt;br /&gt;
				bw.write("     ");&lt;br /&gt;
				&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java_beginners/408788/408914/re-cannot-read-the-files-from-a-directory/#408914</guid>
      <pubDate>Fri, 06 Nov 2009 04:13:26 -0700</pubDate>
      <category>Java Beginners</category>
    </item>
  </channel>
</rss>
