//byte stream, write string to file public static void main(String[] args) throws Exception { // where to output File file=new File("d:"+File.separator+"test.txt"); OutputStream outputStream = new FileOutputStream(file); String str="1234qwer";//The written string byte[] bytes = str.getBytes();//Only byte array can be input, so convert to byte array first outputStream.write(bytes);//写入 outputStream.close(); }
// byte stream, read string from file public static void main(String[] args) throws Exception { // which file to read String path = "d:" + File.separator + "test.txt"; File file = new File(path); InputStream fileInputStream = new FileInputStream(file); byte[] bs = new byte[1000];//All content is read into this array int read_len = fileInputStream.read(bs);//The size of the file read fileInputStream.close(); System.out.println("Length of read" + read_len); System.out.println(new String(bs, 0, read_len));//Output to console }