#java.io
String s1 = "Why so serious!";
String s2="Hello World!☻☺♦";
PrintStream ps = new PrintStream("test.txt");
ps.println(s1);
ps.println(s2);
ps.write(s2.getBytes());
////
FileWriter fw = new FileWriter(test);
fw.write("one more");
fw.write("2222");
fw.flush(); //important
////
FileReader reader = new FileReader(test);
int c;
while((c=reader.read())!=-1){
System.out.print((char)c);
}
FileInputStream fis = new FileInputStream("abc.txt");
byte[] bt = new byte[fis.available()];
fis.read(bt);
System.out.println(new String(bt));
///
try(FileOutputStream fos=new FileOutputStream("C://SomeDir//notes.txt"))
{
// перевод строки в байты
byte[] buffer = text.getBytes();
fos.write(buffer, 0, buffer.length);
}
///
File test = new File("test.txt");
if (!test.exists()) {
test.createNewFile();
}
url = new URL("http://google.com");
urlcon = url.openConnection();
instream = new InputStreamReader(urlcon.getInputStream(), "UTF8");
buff = new BufferedReader(instream);
while (true) {
nextLine = buff.readLine();
if (nextLine != null) {
System.out.println(nextLine);
} else break;
}
Path ff = FileSystems.getDefault().getPath(".", "abc.txt");
try {
List ll =Files.readAllLines(ff);
System.out.println(Arrays.toString(ll.toArray()));
} catch (IOException e) {
e.printStackTrace();
}
File fl = new File("response.json");
File renamedfile = new File("response2.json");
File fl3 = new File("here/test");
File dir2 = new File("response");
System.out.println(fl.exists());
System.out.println(fl.length());
System.out.println(dir2.isDirectory()+" its directory");
String[] dirs = dir2.list();
System.out.println(Arrays.toString(dirs));
// fl.delete();
// fl.renameTo(renamedfile);
fl3.mkdirs(); //make folders here/test
System.out.println(fl.getCanonicalPath());
Path pt = Paths.get("response.json");
System.out.println(Files.isWritable(pt));
FileOutputStream myFile = null;
byte[] test = "testing output".getBytes(Charset.forName("UTF-8"));
try {
myFile = new FileOutputStream("abcOut.txt");
myFile.write(test);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (myFile!=null) try {
myFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream myFile = null;
try {
myFile = new FileInputStream("abc.txt");
boolean eof = false;
while (!eof) {
int byteVal = myFile.read();
System.out.println(byteVal+" ");
if (byteVal==-1) eof=true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (myFile!=null) try {
myFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}