• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why can display the file contents???

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
}
}

}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic