шпаргалки
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

49 lines
1.4 KiB

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package filereaderandwriter2;
import java.io.File;
import java.io.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author denis
*/
public class FileReaderAndWriter2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
File file = new File("SameFile.txt");
if(!file.exists()) try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(FileReaderAndWriter2.class.getName()).log(Level.SEVERE, null, ex);
}
String message = "Hello world";
try (FileWriter writer = new FileWriter(file, true)) {
writer.append("\n" + message);
} catch (IOException ex) {
}
try(FileReader reader = new FileReader(file)){
StringBuffer buffer = new StringBuffer();
char[] cbuf = new char[1024];
int count = 0;
while((count = reader.read(cbuf)) != -1){
buffer.append(new String(cbuf, 0, count));
}
System.out.println(buffer.toString());
}catch(IOException e) {}
}
}