How do I read from a file that is actually being written to?
public class Writer extends Object
{
Writer () {
}
public static String[] strings =
{
"Hello World",
"Goodbye World"
};
public static void main(String[] args)
throws java.io.IOException {
java.io.PrintWriter pw =
new java.io.PrintWriter(new java.io.FileOutputStream("out.txt"), true);
for(String s : strings) {
pw.println(s);
System.in.read();
}
pw.close();
}
}
public class Reader extends Object
{
Reader () {
}
public static void main(String[] args)
throws Exception {
java.io.FileInputStream in = new java.io.FileInputStream("out.txt");
java.nio.channels.FileChannel fc = in.getChannel();
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10);
while(fc.read(bb) >= 0) {
bb.flip();
while(bb.hasRemaining()) {
System.out.println((char)bb.get());
}
bb.clear();
}
System.exit(0);
}
}
Source: By Anthony Cramp as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 3.0
Related code-snippets:
- How do I convert a CSV into an XML file in Java?
- Is Object Property accessible from within object method?
- Do you like compression of files? How do you decompress them?
- What is the meaning of the type safety warning in some Java generics casts?
- What is the difference between Int and Integer in Java and C#?
- What is the best file copy alternative than the Windows defaults?
- I would like to import CSV file to strongly typed data structure in.Net. However the only exception is in one case. Is there any method to import CSV file?
- How can I add custom button in Java?
- What are the different methods to parse strings in Java?
- Is it illegal to throw a Null parameter without a NullPointerException for that parameter?
- What tools are good for code analysis?
- Why is Java autoboxing used only for method invocations and not classes. The type has a class and an operator and the methods are autoboxed, doesn't this mean revocations of the method can be invoked?
- How can I use Java webstart multiple libraries?
- Is it better to use widening vs autoboxing?
- How can I access post variables using Java Servlets?