• 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

how declare a variable global ?

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think

BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));

are the error. but i need the out variable as global...

C:\programa\java\Mysql>javac listfi.java
listfi.java:124: non-static variable out cannot be referenced from a static cont
ext
out.write("Directorio:"+MyrootDir+"\nNombre Favorito: "+wNom
Fav+"\nBASEURL="+wBaseUrl+"\nURL="+wUrl+"\nModified="+wModified+"\nIconFile"+wIc
onFile+"\nIconIndex"+wIconIndex+"\n");
^
listfi.java:164: non-static variable out cannot be referenced from a static cont
ext
out.close();
^
2 errors

C:\programa\java\Mysql>

//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";

BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));

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 String DisplayFileContent(String MyDirectory, String MyCade)
public static void DisplayFileContent(String MyDirectory, String MyCade, int MyrootDir)
{
if (MyDirectory!=null)
{
//File directory = new File ( getCurrentPCFavorites());
File directory = new File (MyDirectory);
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 WFname=f.getName();
//if (WFname.substring(".url")>=0 )
if (f.getName().indexOf(".url")>=0 )
{
System.out.println("[ "+MyDirectory+" ] "+MyCade+"\t"+f.getName()+"\n\n");
BufferedReader in = new BufferedReader(new FileReader(f));
String str;
String wNomFav=f.getName().substring(0,f.getName().indexOf(".url"));
String wBaseUrl="";
String wUrl="";
String wModified="";
String wIconFile="-2";
String wIconIndex="-2";
while ((str = in.readLine()) != null)
{
// break;
//
// System.out.println(str);
//System.out.println("BASEURL=http://webster.cs.ucr.edu/".indexOf(".BASEURL="));
if (str.indexOf( "BASEURL=")>=0)
{
// wBaseUrl = str.substring(str.indexOf( "BASEURL="),str.length()-str.indexOf("BASEURL="));
wBaseUrl = str.substring(str.indexOf( "=")+1);
// System.out.println("BaseUrl= "+str.indexOf( "BASEURL=")) ;
}
else if (str.indexOf( "URL=")>=0)
{
// wUrl = str.substring(str.indexOf( "URL="),str.length()-str.indexOf("URL="));
wUrl = str.substring(str.indexOf( "=")+1);
}
else if (str.indexOf( "Modified=")>=0)
{
// wModified = str.substring(str.indexOf( "Modified="),str.length()-str.indexOf( "Modified="));
wModified = str.substring(str.indexOf( "=")+1);
}
else if (str.indexOf( "IconFile=")>=0)
{
// wIconFile = str.substring(str.indexOf( "IconFile="),str.length()-str.indexOf( "IconFile="));
wIconFile = str.substring(str.indexOf( "=")+1);
}
else if (str.indexOf( "IconIndex=")>=0)
{
// wIconIndex = str.substring(str.indexOf( "IconIndex="),str.length()-str.indexOf( "IconIndex="));
wIconIndex = str.substring(str.indexOf( "=")+1);
}
}
System.out.println("--------------------------------------------------") ;
System.out.println( "Directorio:"+MyrootDir+"\nNombre Favorito: "+wNomFav+"\nBASEURL="+wBaseUrl+"\nURL="+wUrl+"\nModified="+wModified+"\nIconFile"+wIconFile+"\nIconIndex"+wIconIndex);
out.write("Directorio:"+MyrootDir+"\nNombre Favorito: "+wNomFav+"\nBASEURL="+wBaseUrl+"\nURL="+wUrl+"\nModified="+wModified+"\nIconFile"+wIconFile+"\nIconIndex"+wIconIndex+"\n");
System.out.println("--------------------------------------------------") ;
//System.out.println("\n\n"+i+"\n");
in.close();
break;
}
}
catch (IOException e)
{
System.out.println(e);
}
}
else
System.out.println( "Can NOT Read File: " + f.getName());
}
else if (f.isDirectory())
{
// System.out.println( "XXX " + f.getName());
DisplayFileContent(MyDirectory+"\\"+f.getName(), "Dir----- ", MyrootDir++);
//System.out.println( "Directory: " + f.getName());
}
}
}
}
else
{
System.out.println( "Error Directorio Vacio");
}
// return "";
}

public static void main(String[] args)
{
try
{
// File directory = new File("C:\\Temp\\x");
// File directory = new File ( System.getProperty("user.home") + "/favorites");
String PcFavs = getCurrentPCFavorites();
// String PcFavs = "C:\\Temp\\x\\t";
DisplayFileContent(PcFavs,"Fil------ ",0);
out.close();
} catch (IOException e) {
}

}
}
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable out is not static yet you are trying to use it from a static context, a static method in this case. Either access it from a non-static context, such as a non-static method, or change the variable to static.
 
Miguel Enriquez
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i change: BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));

to:

static BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));


and to:

public static BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));

and try compile the program:

C:\programa\java\Mysql>javac listfi.java
listfi.java:11: unreported exception java.io.IOException; must be caught or decl
ared to be thrown
public static BufferedWriter out = new BufferedWriter(new FileWriter("c:\\temp
\\javaRes.txt"));
^
1 error

C:\programa\java\Mysql>

in both cases have a error....

thanks
 
Miguel Enriquez
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
solved/fixed:

private static BufferedWriter out;

and in my constructor:

out = new BufferedWriter(new FileWriter("c:\\temp\\javaRes.txt"));



Thanks to Undernet Java Channel
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic