标签:需要 tostring 写文件 调用 繁体 方法 void stat 上进
FileInputStream fis=null; FileOutputStream fos=null; try { fis=new FileInputStream("D:\\test.txt"); }catch (IOException e) {
e.printStackTrace();
}
fos=new FileOutputStream("D:\\out.txt",true);
一个字节一个字节的读取字节 int n=-1; while ((n=fis.read())!=-1) { sb.append((char)n); } 将byte数组直接声明为输入流的长度,一次性读出所有文字 byte[ ] bytes=new byte[fis.available()]; fis.read(bytes); sb.append(new String(bytes)); 一次读取1024个字节 byte[ ] bytes=new byte[1024]; int n=-1; while (( n=fis.read(bytes)) !=-1){ sb.append(new String(bytes)); } 将字符串转换为byte数组,并通过输出流输入文件 sb.reverse(); fos.write(sb.toString().getBytes()); System.out.println(sb);
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally {
}
public static void main(String[] args) { try { FileInputStream fis=new FileInputStream("D:\\test.txt"); FileOutputStream fos=new FileOutputStream("D:\\test.txt"); int n=-1; while ((n=fis.read())!=-1) { fos.write(n); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
作用
FileInputStream fis=null; FileOutputStream fos=null; BufferedInputStream bis=null; BufferedOutputStream bos=null; long date1=new Date().getTime(); try {
bis =new BufferedInputStream(new FileInputStream("D:\\test.txt"));//这种写法,我们成为IO链,IO关闭时只需要关闭最外层流,内层流将自动关闭
bos=new BufferedOutputStream(new FileOutputStream("D:\\out.txt",true)); StringBuffer sb=new StringBuffer(); //一个字节一个字节的读取字节 int n=-1; while ((n=fis.read())!=-1) { sb.append((char)n); } System.out.println(sb); long date2=new Date().getTime(); System.out.println("--------"+(date1-date2)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { // fis.close(); bis.close(); // fos.close(); bos.flush();//在程序最后,刷新缓存流,将缓存流中未满的内容,写入到文件中 bos.close();//调用close方法,将自动刷新 } catch (IOException e) { e.printStackTrace(); } }
String name="hhh"; int age=23; double height=134.6; String ads="yantai"; DataInputStream dis=null; DataOutputStream dos=null; try { dos=new DataOutputStream(new FileOutputStream("d:\\test.txt")); dos.writeUTF(name); dos.writeInt(age); dos.writeDouble(height); dos.writeUTF(ads); dis=new DataInputStream(new FileInputStream("d:\\tout.txt")); String uname = dis.readUTF(); int uage = dis.readInt(); double uheight = dis.readDouble(); String uads = dis.readUTF(); System.out.println(uname+"---"+uage+"---"+uheight+"---"+uads); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { dis.close(); dos.flush(); dos.close(); } catch (IOException e) { e.printStackTrace(); } } }
public static void main(String[] args) { person zhangsan=new person("张三", 28, 178.5, "烟台"); ObjectInputStream ois =null; ObjectOutputStream oos =null; try { oos=new ObjectOutputStream(new FileOutputStream("d:\\test.txt")); oos.writeObject(zhangsan); ois=new ObjectInputStream(new FileInputStream("d:\\test.txt")); person p=(person)ois.readObject(); System.out.println(p); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { ois.close(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
class person implements Serializable{ private static final long serialVersionUID = 2545980081191079819L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public person(String name) { super(); this.name = name; } }
public static void main(String[] args) { FileWriter fw=null; FileReader fr=null; try { fw=new FileWriter("D:/io.txt"); String s="1358kkkkk将"; for (int i = 0; i < s.length(); i++) { fw.write(s.charAt(i)); } fw.flush(); fr=new FileReader("D:/io.txt"); int n=0; StringBuffer sb=new StringBuffer(); while ((n=fr.read())!=-1 ){ sb.append((char)n); } System.out.println(sb.toString()); } catch (IOException e) { e.printStackTrace(); }finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
bw=new BufferedWriter( new OutputStreamWriter( new FileOutputStream("D:/out.txt"),"UTF-8")) ;//表示用UTF-8对字符串进行解码为字节数组 bw.write(sb.toString()+"kkkkkkkkkkkkkkkkkkkkk");
String s=sb.toString()+"kkkkkkkkkkkkkkkkkkkkk"; s=new String(s.getBytes("UTF-8"),"GBK");//表示将解码后的字节数组 重新使用GBK的编码,组合成字符串 bw.write(s);
最终,一个UTF-8的字符串,经过解码编码转换为GBK格式的字符串
标签:需要 tostring 写文件 调用 繁体 方法 void stat 上进
原文地址:https://www.cnblogs.com/fylong/p/8904341.html