Content received from: http://JavaFAQ.nu/java-article325.html
Java Newsletters Archive: 6 Wednesday, January 28, 2004 (16:03:45)
Posted by jalex
Hello dear subsribers!
We continue to send you our daily Java tips!
Today you receive our tips that will be published
on our site in October, 18 and following week.
This is not sent unsolicited. You are receiving this
newsletter because you signed up for it, or a friend has
forwarded it for you.
******************************************************
Please, if you like our tips, recommend us to your friends and colleagues.
******************************************************
Tip1
How does Java read the text files? Can Java read the files that are in
other formats? Is the read file method in Java only recognizes the
file in .txt or other text format?
Answer: Java can read any text file (using a PrintWriter for example),
the attribute at the end is an indictor and thus is not relevant as long as the
actual code read is in the correct format. It can read files that are in other
formats bytes etc and if you have a wierd format you could extend the IO
mechanism with some work to work with that.
......................................................................………………………..
Tip 2
How can I take a program that runs in a DOS shell and send the
text that comes from the shell program into a Java program where
it can analyzed, etc.?
Answer: From a command line, use a pipe (with the "|" symbol):
c:> dosprogram | java JavaProgram
In the Java program, read the text from System.in:
public static void main(String[] args) throws IOException {
int nLines = 0;
BufferedReader in =
new BufferedReader(
new InputStreamReader( System.in));
for (; {
String line = in.readLine();
if (line == null)
break;
nLines++;
System.out.println(nLines + ":" + line);
}
}
......................................................................………………………..
Tip 3
Q: When creating a new file, is it possible to control whether or not
an existing file with the same name is/is not overwritten? I haven't
been able to answer this by looking at the java.io API…
Answer: There is a method in File to atomically create a new file that will
fail if the file exists... You can use this to try creating the file, and if it
exists
already don't ever open the FileOutputStream to write contents.
......................................................................………………………..
Tip 4
How does a java application stored within a jar file reference/edit/read
other files (like .txt, or data files,) that are also within the jar file?
Answer: Classes located in a JAR archive are loaded via a class loader
whose purpose is to load classes form JAR archives. This ClassLoader
implements the getResource and getResourceAsStream methods to
retrieve files from the JAR file. So you can take any class from the JAR
and say ClassName.class.getClassLoader().getResource("fname");
to get the resource and use it.
......................................................................………………………..
Tip 5
If there is a way to run a java program by just typing the name in
UNIX? I mean instead of typing for example "java Main" just type
"Main" and run the program.
And how to implement that in a makefile?
Answer: Write a script that runs the program and put it in your path. For
instance:
#!/bin/sh
java BlahBlah
Call this whatever you want, mv it to your /usr/local/bin directory, then just
type it at the command line and BlahBlah will be run.
......................................................................………………………..
Tip 6
Could someone show me a basic File I/O example? I just can't figure out streams.
I'm willing to accept basic mockery in exchange...
Answer:
import java.io.*;
public class FileIO {
public static void main(String[] args) throws Exception {
if(args.length!=1){
System.out.println("Invalid parameters!!!");
System.exit(0);
}
File fl = new File(args[0]);
FileReader fileReader = new FileReader(fl);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String currentLine;
while( (currentLine = bufferedReader.readLine()) != null ){
System.out.println(currentLine);
}
}
}
......................................................................………………………..
Tip 7
I want to keep my java GUI always on the top of any other desktop
application. Any idea?
Answer: Spawn a thread that knows about the parent Window,
and every X milliseconds, executes the toFront () command of
that window. Just remember to execute it using
SwingUtilities.invokeLater (), and don't
let your users launch two apps, unless you enjoy screen lockup!
With best regards Java FAQ team, http://javafaq.nu/java
Free Daily Java Tips, free books, code examples and hand-selected links!
You can find our tips on site later also!
Recommend us to your friends!
|