简体   繁体   中英

How to write String from variable to text file

My goal here is to read lines from a text file, check if they are palindromes, and then write those to a completely different file.

Now the problem, as far as I can see, lies in the if statement block where I check for palindromes successfully but can't seem to write them to another file because they are stored in a variable.

When I use the BufferedWriter write method and set the parameters as an actual string with quotes, everything works.

How can I solve this?


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

public class Zadatak14 {

    public static void main(String[] args) {

        BufferedReader br;

        try {

            br = new BufferedReader(new FileReader("C:\\Users\\Luka\\Documents\\NetBeansProjects\\CS101-DZ14-14\\src\\zadatak14\\ulaz.txt"));

            String line;
            

            while ((line = br.readLine()) != null) {

                String palindrome = new StringBuilder(line).reverse().toString();

                BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Luka\\Documents\\NetBeansProjects\\CS101-DZ14-14\\izlaz.txt"));
                
                
                
                
                if (line.contains(palindrome)) {

                    System.out.println(line);

                    bw.write(line);

                }

                bw.close();

            }

            br.close();

        } catch (FileNotFoundException e) {
            System.out.println("Greska");
        } catch (IOException ex) {
            System.out.println("Greska");
        }
    }

}

BufferedWriter bw = new BufferedWriter

You're making a new writer for every line in your input , which surely you don't want. Move the creation of the writer up top, right after opening the reader.

br.close();

not how you do that. This is how you do that:

try (BufferedWriter bw = new BufferedWriter(new FileWriter("...."))) {
   // code here
}
// no matter how you get out of this code, the file is closed.

The reason you do it with the above is because that bw.close() won't get called if exceptions occur, if you return out of the block. With the try construct, that stream is closed when code exits from the block no matter how it does that.

} catch (FileNotFoundException e) {
            System.out.println("Greska");

Not how you do that - exceptions contain a lot of useful info (at least 4 things, more for some specific kinds of exceptions: type, message, stack trace, cause), and you're tossing it all in the garbage. It's a bad idea to toss information about problems in the bin. Don't. The right move is to add throws Exception to your public static void main(String[] args) method, and then you don't need a catch at all. If you must, the correct body for a catch block if you don't have a good way to handle the problem, is throw new RuntimeException("Uncaught", e); . This preserves all information and doesn't cause errors about having to catch exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM