work with text files in java
I have just started learning java after C + +. but so far nothing is clear. especially the work with classes.
tell me please how to open a file for reading or writing? it is desirable to provide the code completely.
Thanks. sorry for bad english.
upd: I started to learn java hour ago, but has not yet written a working program.
开发者_JAVA百科Thank you all for your answers!
Maybe you should read the IO part of the sun java tutorial first.
E.g. Reading, writing and creating Files
May the Apache Commons be your friend. There's a FileUtils class that does the job.
Best u go through java complete reference book.... I give sample program i hope this will help u....
public class Abc {
public static void main(String[] args) {
Abc.modemp();
}
public static void modemp() {
String detail;
try {
BufferedReader empdtil = new BufferedReader(new FileReader("File Location"));
while ((detail = empdtil.readLine()) != null) {
System.out.println(detail);
}
empdtil.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
Here is a great set of tutorials for learning Java: The Java Tutorials.
For a tutorial on how to do I/O, look at the chapter Basic I/O from those tutorials.
I suggest you avoid the BufferedReader for reading a text InputStream: readLine consumes the eol character and it is not recoverable (could have been, \n, \r, \n\r, ...). If you want to preserve eol characters, you need to work directly with an InputStreamReader. Here's what I use:
/**
* Read the open UTF-8 text InputStream to the end and close.
* @param stream the open input stream
* @return the text
*/
public static String readText(InputStream stream) {
return readText(stream, "UTF-8");
}
/**
* Read the open text InputStream to the end and close.
* @param stream the open text stream, not null
* @param charsetName the charset (e.g. UTF-8), must be valid
* @return the text
* @throws RuntimeException wrapping UnsupportedEncodingException (if charsetName invalid) or IOException (if problem reading stream)
*/
public static String readText(InputStream stream, String charsetName) {
InputStreamReader reader = null;
try {
reader = new java.io.InputStreamReader(stream, charsetName);
StringBuilder response = new StringBuilder();
char[] buffer = new char[4 * 1024];
int n = 0;
while (n >= 0) {
n = reader.read(buffer, 0, buffer.length);
if (n > 0) {
response.append(buffer, 0, n);
}
}
return response.toString();
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(stream, reader);
}
}
/**
* Close the closeable.
* @param obj the closeable, may be null
* @return true if successfully closed
*/
public static boolean close(Closeable obj) {
if(obj != null) {
try {
obj.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
else
return false;
}
/**
* Close all the closeables
* @param objs the closeables, the list may not be null but individual items may
* @return true if all were closed successfully
*/
public static boolean close(Closeable... objs) {
boolean success = true;
for(Closeable c : objs)
success = success && close(c);
return success;
}
There are several ways to obtain an InputStream for a file, one way is using one of the FileInputStream constructors.
Writing to file is a bit easier, with the use of PrintWriter:
File file = new File("C:\\file.txt");
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter(file, true));
writer.print("hello ");
writer.println("world");
writer.print(12.23);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(writer);
}
精彩评论