How do I make this work?

N

newbie_at_java

Dear Zefria,

thank you for your suggestion. O miracle, it works (but only when used with a main method).
Not when I place the code in class: Solution_1.java and call this code from class: Use_solution_1.java (as you can see both are in the same package). Do you know how I make this work?

This is what I do:

Use_solution_1.java:

package solutions;

import java.io.*;
import solutions.Solution_1;

public class Use_solution_1
{
public void main(String[] args)
{
FileReadingMethod("c:\\temp\\this_will_never_work.txt");
}

}

Solution_1.java:

package solutions;

import java.io.*;
import static java.lang.System.out;


public class Solution_1 {

public void FileReadingMethod(String filename)
// public static void main(String args[])
{
BufferedReader br = null;
String line;
// String filename = "c:\\temp\\this_will_never_work.txt";
try
{
br = new BufferedReader(new FileReader(filename));
}
catch (FileNotFoundException e)
{
System.err.println("Requested file was not found!");
System.exit(1);
}
try
{
line = br.readLine();
while (true)
{
/////////
// PROCESSING CODES GOES HERE
/////////
out.println("Gelezen= "+line);
// Read the next line. If line==null, that indicates the end of the file.
line = br.readLine();
if (line == null) break;
}
}
catch (IOException e)
{
System.err.println("File was found, but an IOError occured!");
System.exit(2);
}
}
}




--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 
A

Andrew Thompson via JavaKB.com

newbie_at_java said:
Dear Zefria,

Who? Note that your message is going to a public
forum, any comment to a usenet newsgroup is
effectively addressed to 'everybody' that reads it.
....O miracle, it works....

Code generally works (or breaks) according to well
known specifications, as opposed to the whims of
Gods, or influence of other magics.
Not when I place the code in class: Solution_1.java and call
this code from class: Use_solution_1.java (as you can see
both are in the same package). Do you know how I make this work?

Well.. I cannot speak for Zefria, but I saw this output
after a number of changes to the code shown...

D:\projects\>java solutions.Use_solution_1
Requested file was not found!

..so, sure.

Did you have a specific question beyond that?
A question you might like to address to 'everyone
who cares to read or reply'?

What exact compilation errors are you getting?
What do you suspect are the cause of those errors?
Where did you try researching it further (books, links
etc.)?

In the meantime - couple of notes,

Using Strings to represent a file name is very fragile,
I feel that the only parameters for file or URL based
resources should be File or URI objects, so I would
suggest changing the method 'FileReadingMethod'
to accept a File or URI, rather than a String.

The convention for method names would make that
method as..
fileReadingMethod
..with a lower case first letter.

To build up the file itself, it is better to use the
File(File, String) constructor in a loop to gain a
subdirectory for the actual file required. Using that
constructor, the correct separator will be automatically
be used for Windows, *nix or Mac.

For getting to a system 'temp' style directory,
there are system properties available that point
directly there (to a TEMP dir appropriate to the
OS).

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
L

Lew

Andrew said:
To build up the file itself, it is better to use the
File(File, String) constructor in a loop to gain a
subdirectory for the actual file required. Using that
constructor, the correct separator will be automatically
be used for Windows, *nix or Mac.

I don't know why posts show Windows paths using the backslash anyway; the
forward slash works just fine for Windows and has done so for, what, seven
years or so?

Pteh.
 
A

Andrew T. via JavaKB.com

Lew said:
..
I don't know why posts show Windows paths using the backslash anyway; the
forward slash works just fine for Windows and has done so for, what, seven
years or so?

What about 7 years in the future (or even 1)?
I would be more worried that MS (or, stretching
the imagination, another OS) might decide to
change their separator in future.

Long ago I decided to put my trust in the JVM &
Sun's engineers to come up with the logical
separator for that OS, and possibly for that moment.
If it changes, I am guessing Sun has the sense to
issue an 'update patch' for any OS or JRE version
that needs it. (Well OK - Sun do not directly
control the Mac. VM, but I am hoping the
Apple folks can produce the correct separator
for the JRE on Macs.!)

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
D

Daniel Gee

newbie_at_jav, your mistake is a trivial one and I'm surprised that no
one else caught it: The method as you renamed it isn't a static
method. So, as a result, you need to call "FileReadingMethod" on an
instance of Solution_1 after having made one.

Solution_1 s1 = new Solution_1();
s1.FileReadingMethod("/tmp/test.txt");

As to using Strings instead of Files, Andrew, the decision comes from
the fact that the filename was being read from the args of the
program, and it just wasn't quite worth the time to convert over to a
File object compared to the possibility of just testing if it was
going to work and then letting it break 5 lines into execution if the
filename was typed in wrong.

However, in the future newbie_at_jav, You should definately look into
the File class: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

Perhaps something fun like this:

import java.io.File;
public class RandFile
{
public static void main(String[] args)
{
File di;
if(args.length == 0) di = new File(".");
else di = new File(args[0]);

String[] subitems = di.list();
File targ = new File(subitems[(int)
(Math.random()*subitems.length)]);

while(targ.isDirectory())
{
targ = new File(subitems[(int)(Math.random()*subitems.length)]);
}

System.out.println(targ);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top