标签:and int void write class tput get 写入文件 buffere
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.nio.CharBuffer; import java.util.ArrayList; import java.util.List; public class filedemo { public static void main(String[] args) { File file=new File("c:\\22ssssssssss.txt"); try { if(!file.exists()){ file.createNewFile(); FileOutputStream fileOutputStream=new FileOutputStream(file); DataOutputStream fDataOutputStream=new DataOutputStream(fileOutputStream); //fDataOutputStream.write("sfs哈d".getBytes()); fDataOutputStream.writeUTF("\nx哈哈"); fDataOutputStream.close(); fileOutputStream.close(); } else { // 注意1:一般情况下在读入时尽量按照写入时的格式进行读取, // 否则有可能会出现显示乱码或程序出现异常。 // 如首先写入文件用的是writeUTF(),在读取的时候如果不是用readUTF()就会出现乱码, // 如果readUTF()读取的内容不是UTF-8格式的,程序就会抛出异常。 FileInputStream fileInputStream=new FileInputStream(file); DataInputStream dataInputStream=new DataInputStream(fileInputStream); //byte [] b2=new byte[1024*120]; //int len=dataInputStream.read(b2); //按字节读取剩余的内容 // System.out.println(new String(b2,0,len)); System.out.println(dataInputStream.readUTF()); dataInputStream.close(); fileInputStream.close(); } } catch (Exception e) { // TODO: handle exception } } /** * 缓存流 * @param args */ public static void main3(String[] args) { File file=new File("c:\\22ss.txt"); try { if(!file.exists()){ file.createNewFile(); FileWriter fWriter=new FileWriter(file); BufferedWriter bWriter=new BufferedWriter(fWriter); String string="sfjsafjas婚纱福建\n"; bWriter.write(string); bWriter.write(string); bWriter.close(); fWriter.close(); } else { FileReader fReader=new FileReader(file); BufferedReader bufferedReader=new BufferedReader(fReader); while(bufferedReader.ready()){ String string= bufferedReader.readLine(); System.err.println(string); } bufferedReader.close(); fReader.close(); } } catch (Exception e) { // TODO: handle exception } } /** * 字符流 * @param args */ public static void main2(String[] args) { File file=new File("c:\\demo1fsdfsd.txt"); try { if(!file.exists()){ file.createNewFile(); FileWriter fWriter=new FileWriter(file); String string="sfjsafjas婚纱福建"; fWriter.write(string); fWriter.close(); } else { FileReader fReader=new FileReader(file); char[] c=new char[1024]; StringBuffer sBuilder=new StringBuffer(); int len=fReader.read(c); while(len!=-1){ fReader.read(c, 0, len); sBuilder.append(c); len=fReader.read(); System.out.println(new String(c)); } } } catch (Exception e) { // TODO: handle exception } } /** * 字节流 * @param args */ public static void main1(String[] args) { File file=new File("c:\\demo1.txt"); try { if(!file.exists()){ file.createNewFile(); FileOutputStream foStream=new FileOutputStream(file); byte[] b=" sjd横槊赋诗防守打法试试是".getBytes(); foStream.write(b); foStream.close(); } else { FileInputStream fisFileInputStream=new FileInputStream(file); byte[] b=new byte[fisFileInputStream.available()]; fisFileInputStream.read(b); System.out.println(new String(b)); fisFileInputStream.close(); } } catch (IOException e) { // TODO: handle exception System.err.println(e.getMessage()); } } }
标签:and int void write class tput get 写入文件 buffere
原文地址:http://www.cnblogs.com/wlzhang/p/7868073.html