• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how declare a variable global ?  RSS feed

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!