文件路徑:
public static final String FILEPATH= File.separator+"Users"+ File.separator+"xuminzhe"+File.separator+"Documents"+File.separator+"io";
?
1.創建文件
?
public static void main(String[] args) {File file=new File(Constant.FILEPATH+File.separator+"io.text");try {boolean newFile = file.createNewFile();} catch (IOException e) {e.printStackTrace();}}
?
2.查找指定目錄下文件
?
public static void main(String[] args) {File file=new File(Constant.FILEPATH);File[] str = file.listFiles();for (int i = 0; i < str.length; i++) {System.out.println(str[i]);}}
?
3.文件流-寫入
?
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);OutputStream outputStream=new FileOutputStream(file,true);byte[] bytes = "你好".getBytes();for (int i = 0; i < bytes.length; i++) {outputStream.write(bytes[i]);}outputStream.close();
?
4.文件流-讀取
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);InputStream inputStream=new FileInputStream(file);/*** 單字節讀取*/{byte[] bytes = new byte[1024];int read1;int count =0;while((read1 = inputStream.read())!=-1){bytes[count++]=(byte) read1;}System.out.println(new String(bytes));}/*** 多字節讀取*/{byte[] bytes=new byte[(int) file.length()];int read;while((read=inputStream.read(bytes))!=-1){System.out.println(new String (bytes));}}}
?
5.字符流-寫入
?
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);Writer writer=new FileWriter(file,true);String str="\r\nhello";writer.write(str);writer.close();
?
6.字符流-讀取
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);Reader read=new FileReader(file);char[] ch=new char[100];int temp=0;int count=0;while((temp=read.read())!=(-1)){ch[count++]=(char)temp;}read.close();System.out.println("內容為"+new String(ch,0,count));}
?
7.轉換流-寫入??將輸出的字符流轉化為字節流
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);Writer writer=new java.io.OutputStreamWriter(new FileOutputStream(file,true));writer.write("kobe");writer.close();}
?
8.轉換流-讀取?將輸入的字節流轉換為字符流
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);Reader read = new InputStreamReader(new FileInputStream(file));char[] b=new char[100];int len=read.read(b);System.out.println(new String(b,0,len));read.close();}
?
9.對象流
?
static String filename=Constant.FILEPATH+ File.separator+"HELLO.text";static File file=new File(filename);public static void main(String[] args) throws Exception {serializable(file);deserializable(file);}/*** 反序列化* @param file* @throws IOException* @throws ClassNotFoundException*/private static void deserializable(File file) throws IOException, ClassNotFoundException {ObjectInputStream stream=new ObjectInputStream(new FileInputStream(file));Person o = (Person) stream.readObject();System.out.println(o.toString());}/*** 序列化對象* @param file* @throws IOException*/private static void serializable(File file) throws IOException {ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(file,true));outputStream.writeObject(new Person("xmz",13));outputStream.close();}
?
10.緩沖字符流-讀取
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);BufferedReader bufferedReader=new BufferedReader(new FileReader(file));String line;while((line=bufferedReader.readLine())!=null){//讀取一個文本行 System.out.println(line);}bufferedReader.close();}
?
11.緩沖字符流-寫入
?
public static void main(String[] args) throws IOException {String filename=Constant.FILEPATH+ File.separator+"HELLO.text";File file=new File(filename);FileWriter fileWriter=new FileWriter(file);/*** 為了提高寫入的效率,使用了字符流的緩沖區。* 創建了一個字符寫入流的緩沖區對象,并和指定要被緩沖的流對象相關聯。*/BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);bufferedWriter.write("jordan喬丹");bufferedWriter.newLine();//換行bufferedWriter.write("kobe蝸殼");bufferedWriter.write("wade韋德");bufferedWriter.flush();bufferedWriter.close();}
12 管道流-可用于線程通信
?
static class Send implements Runnable{private PipedOutputStream out=null;public Send() {out=new PipedOutputStream();}public PipedOutputStream getOut(){return this.out;}public void run(){String message="hello,xmz";try{out.write(message.getBytes());}catch (Exception e) {e.printStackTrace();}try{out.close();}catch (Exception e) {e.printStackTrace();}}}/*** 接受消息類* */static class Recive implements Runnable{private PipedInputStream input=null;public Recive(){this.input=new PipedInputStream();}public PipedInputStream getInput(){return this.input;}public void run(){byte[] b=new byte[1000];int len=0;try{len=this.input.read(b);}catch (Exception e) {e.printStackTrace();}try{input.close();}catch (Exception e) {e.printStackTrace();}System.out.println("接受的內容為 "+(new String(b,0,len)));}}public static void main(String[] args) throws IOException {Send send=new Send();Recive recive=new Recive();try{//管道連接 send.getOut().connect(recive.getInput());}catch (Exception e) {e.printStackTrace();}new Thread(send).start();new Thread(recive).start();}
?