I am just trying to reverse the lines which I receive from the input, but every single time I run my code, the output.txt file is empty. What am I missing?
It appears mostly correct to me, even the recursion passage.
Thanks
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class ReverseLines {
public static BufferedReader input;
public static PrintWriter output;
public static void main(String[] args) throws Exception{
input = new BufferedReader(new FileReader(args[0]));
output = new PrintWriter(new FileWriter(args[1]));
reverse(input, output);
}
public static void reverse( BufferedReader input, PrintWriter output)
throws Exception {
String line = input.readLine();
if(line != null) {
reverse (input, output);
output.println(line);
}
}
}
Close the PrintWriter in your main method:
output.close();
do output.flush() and check whether it works!
Related
I'm trying to make two objects that are able to read and write to a text file. The problem happens when I execute the program. The text file gets erased. Why is this happening and how do I fix this?
CODE:
Object to read text files:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Reader {
public static String fromFile;
public static FileReader fr;
public static BufferedReader file;
public Reader(String fileName) throws IOException, FileNotFoundException {
fr = new FileReader(fileName);
file = new BufferedReader(fr);
}
public void readFile() throws IOException {
while((fromFile = file.readLine()) != null) {
System.out.println(fromFile);
}
}
}
Object to write to text files:
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class Writer {
public static FileWriter fw;
public static BufferedWriter file;
public Writer(String fileName) throws IOException, FileNotFoundException {
fw = new FileWriter(fileName);
file = new BufferedWriter(fw);
}
}
Main method to create Reader and Writer objects and use the Reader to read the text file:
import java.io.IOException;
public class Main {
public static Writer toFile;
public static Reader fromFile;
public static String fileName = "test123.txt";
public static void main(String[] args) throws IOException {
toFile = new Writer(fileName);
fromFile = new Reader(fileName);
fromFile.readFile();
}
}
This is just my suggestion.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
public static String fileName = "test123.txt";
Read rd=new Read();
rd.reader(filename);
}
}
public class Read
{
public void reader(String filename)
{
FileReader fr=new FileReader(filename);
BufferedReader bfr=new BufferedWriter (fr);
String text="";
String line=reader.readLine();
while(line!=null)
{
text+=line;
line=reader.readLine();
}
System.out.println(text);
}
fr.close();
bfr.close();
}
I would like to know how to write all lines from the java output in a .txt file.
I've done some tests so far but I don't seem to be able to find the solution :/
Here is a small code, if you could show me with this one, it would be greatly appreciated :
The code shown below asks the user what to write in a .txt file but I want it to write all the printed lines in a .txt file without asking the user anything. Thank you
package test;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class Test {
public static void main(String[] args)throws Exception {
System.out.println("Hello");
System.out.println("Hi");
System.out.println("Hola");
System.out.println("Bonjour");
System.out.println("Hallo");
System.out.println("Hej");
System.out.println("Alo");
System.out.println("Ciao");
writeOutput();
}
public static void writeOutput() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String lineFromInput = in.readLine();
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
out.println(lineFromInput);
out.close();
}
}
Use directly PrintStream to write the String values.
public static void main(String[] args)throws Exception {
PrintStream printStream = new PrintStream(new File("output.txt"));
// hook for closing the stream
Runtime.getRuntime().addShutdownHook(new Thread(printStream::close));
// writing
write(printStream,"Hello", "Hi", "Hola", "Bonjour", "Hallo", "Hej",
"Alo","Ciao");
// writing again
write(printStream, "A new String", "And again another one...");
}
public static void write(PrintStream printStream, String... values) throws Exception {
try{
for (String value : values){
printStream.println(value);
}
printStream.flush();
}
catch (Exception e){
// handling exception
}
}
}
java test.Test > somefile.txt
I'm trying to submit the second code on spoj but it gives wrong answer but the first one is accepted although i think that the logic of the two codes are the same.
public class Main {
public static void main(String[] args) throws java.lang.Exception {
java.io.BufferedReader r = new java.io.BufferedReader(
new java.io.InputStreamReader(System.in));
String s;
while (!(s = r.readLine()).startsWith("42"))
System.out.println(s);
}
}
and
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
if (n != 42) {
System.out.println(n);
}
}
}
There is no loop in your second code. Try your code using the following input data:
1
2
88
42
99
Your second code is going to process only first line on the input (i.e. 1). Here is the working example of your code: http://ideone.com/Qr1q3N
You can, for example, introduce a loop in the following way:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
while ((n = Integer.parseInt(br.readLine())) != 42) {
System.out.println(n);
}
}
}
Here you can see this code in action: http://ideone.com/z8H4fP
I need done in the most simplest way possible..... And where have I gone wrong here....
This is my code so far...
import java.io.*;
class test
{
public static void main()throws IOException
{
FileReader f=new FileReader("g.txt");
BufferedReader in=new BufferedReader(f);
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("g.txt")));
String ar[]=new String[5];
String text;int i=0;
while((text=in.readLine())!=null)
{
ar[i]=text;
i++;
}
i=0;
for(i=0;i<ar.length;i++)
{
if(ar[i].equals("the da vinci code"))
{
ar[i]=null;
break;
}
}
for(int j=0;j<ar.length;j++)
{
System.out.println(ar[j]);
p.println(ar[i]);
}
in.close();
p.close();
}
}
Here is one way though better is to write to another file as you read from this, have a try -catch and close the file handles in the finally, use logging ...
package files;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;//growing array
class ReadFileRemoveLines// class test is a bad name
{
public static void main(String[]args)throws IOException//need to follow the signature or cant start the program
{
FileReader f=new FileReader("g.txt");
BufferedReader in=new BufferedReader(f);
ArrayList<String> ar = new ArrayList<String>(5);//initial size if you expect so many lines or leave out the 5 ()
String text;int i=0;
while((text = in.readLine()) != null)//spaces between symbols help readability
{
//why not test test here
if(!text.equals("the da vinci code")){
ar.add(text);
}
///i++;
}
/*
i=0;
for(i=0;i<ar.length;i++)
{
if(ar[i].equals("the da vinci code"))
{
ar[i]=null;
break;
}
}*/
in.close();
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("g.txt")));
for(int j=0;j<ar.size();j++)
{
System.out.println(ar.get(j));
p.println(ar.get(j));//why are you printint of i loop is j
}
p.close();
}
}
Just making the underlying reference of that line null does not delete that line in that file.
You need to read the file completely.
Pick up the lines which you want and store them in a collection.
Rename or move that file(Create a backup just in case).
Write all the lines again with the same file name.
Okay I may be doing something stupid or this should be a simple fix but basically I have a text file I am reading from with a scanner object and I am getting a nullpointer exception when I reach the end of the file I was wondering how to get fix this
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Scanner;
public class FileAccess {
public static void main (String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("move_list.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
}
finally {
if (s != null) {
s.close();
}
}
}
}
Once it reaches the end I get:
Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.toMatchResult(libgcj.so.10)
at java.util.Scanner.myCoreNext(libgcj.so.10)
at java.util.Scanner.hasNext(libgcj.so.10)
at FileAccess.main(FileAccess.java:13)
Try using
while (s.hasNextLine()) {
String line = s.nextLine();
System.out.println(line);
}
s.close();