Erinevus lehekülje "Java:Faili kirjutamine" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
P (Ago teisaldas lehekülje Java:Failist kirjutamine pealkirja Java:Faili kirjutamine alla)
 
(Erinevus puudub)

Viimane redaktsioon: 7. märts 2015, kell 02:25

<source lang="java"> import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption;

public class WriteFile { public static final String FILENAME = "test.txt";

public static void main(String[] args) throws IOException { Path path = Paths.get(FILENAME);

// BufferedWriter BufferedWriter writer = Files.newBufferedWriter(path); // note the \n in the end of the string. // this adds a new line in the end of the first string. // without this the file would become "teretere" // currently its "tere" in the first line and "tere" on the second. writer.write("tere\n"); writer.write("tere"); writer.close();

// FileWriter FileWriter writer2 = new FileWriter(FILENAME); writer2.write("" + 4); // "4" writer2.close();

// PrintWriter + append // providing additional option APPEND, // we can write in the end of the existing file PrintWriter printWriter = new PrintWriter( Files.newBufferedWriter(path, StandardOpenOption.APPEND)); // PrintWriter has already familiar methods like "println" // which add line break printWriter.println("some text"); printWriter.println("other line"); printWriter.close(); }

} </source>