• Post Reply Bookmark Topic Watch Topic
  • New Topic

why can display the file contents???

 
Miguel Enriquez
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See lines:
System.out.println( "Can Read File: " + f.getName());
in the console, display: Can Read File: www.tabledance.com.mx.url by sample but why not enter to the loop for display the content of the file??

thanks

Here the code:



import java.io.*;

public class listfi {

private static final String REGQUERY_UTIL = "reg query ";
private static final String REGSTR_TOKEN = "REG_SZ";
private static final String COMPUTER_WINDOWS_FAVORITES_FOLDER = REGQUERY_UTIL +
"\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v Favorites";
public static String getCurrentPCFavorites()
{
try {
Process process = Runtime.getRuntime().exec(COMPUTER_WINDOWS_FAVORITES_FOLDER);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1)
return null;
return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
return null;
}
}

static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;

StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}

public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { ; }
}

String getResult() {
return sw.toString();
}
}

public static void main(String[] args)
{
// File directory = new File("C:\\Temp\\x");
// File directory = new File ( System.getProperty("user.home") + "/favorites");
File directory = new File ( getCurrentPCFavorites());
File[] filesInDir = directory.listFiles();
if (filesInDir != null) {
int length = filesInDir.length;
for (int i = 0; i < length; ++i)
{
File f = filesInDir[i];
if (f.isFile())
{
if ( f.canRead() )
{
System.out.println( "Can Read File: " + f.getName());
try
{
BufferedReader in = new BufferedReader(new FileReader(f.getName()));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(str);
}
in.close();
}
catch (IOException e)
{
}
}
else
System.out.println( "Can NOT Read File: " + f.getName());
}
else if (f.isDirectory())
{
System.out.println( "Directory: " + f.getName());
}
}
}
}

}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These three lines are about the worst thing a programmer can do:



You're swallowing any IOExceptions that occur while reading the file, and not even reporting the problem. You must, at a minimum, log the error so that you'll know what's gone wrong! In any case, most likely that's excactly what's happening here: the file open, or the first read, is throwing an exception, and you're simply discarding the useful explanation of what's gone wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!