To skip a line when writing to a file using a FileWriter
in Java, you can use the write
method and pass it a newline character \n
.
Here's an example of how you can do this:
import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { try (FileWriter writer = new FileWriter("/path/to/file.txt")) { writer.write("Line 1\n"); writer.write("Line 2\n"); } catch (IOException e) { e.printStackTrace(); } } }
This code will write the strings "Line 1" and "Line 2" to the file, with a newline character after each line.
Alternatively, you can use the println
method of the PrintWriter
class, which automatically appends a newline character after each line:
import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Main { public static void main(String[] args) { try (PrintWriter writer = new PrintWriter(new FileWriter("/path/to/file.txt"))) { writer.println("Line 1"); writer.println("Line 2"); } catch (IOException e) { e.printStackTrace(); } } }
This code will have the same effect as the previous example, but it uses the PrintWriter
class to write to the file.