Java IO API記錄

文件路徑:

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();}

?

轉載于:https://www.cnblogs.com/xmzJava/p/8987053.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452777.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452777.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452777.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Flask項目--預防csrf攻擊原理

1.CSRF機制原理 2.csrf成功攻擊示意圖 3.csrf防御

數據管理技術的發展過程

人工管理階段&#xff08;20世紀50年代中之前&#xff09;文件系統階段&#xff08;20世紀50年代末--60年代中&#xff09;數據庫系統階段&#xff08;20世紀60年代末--現在&#xff09;

一次失敗的項目經理招聘經驗

成功的原因有許多種&#xff0c;而失敗的原因往往就那么幾種。人們更愿意去討論自己是如何成功的&#xff0c;確不太情愿開誠布公的去剖析自己是如何失敗的。而時刻去反思自己失敗的案例&#xff0c;我們會進步的更快&#xff01; 和大家分享一個2010年發生在我身邊的真實案例&…

NG客制項目下的I18n國際化標準方案

方案選擇 國際化i18n ? 這個方案是最成熟的&#xff0c;同時也是官方的方案&#xff0c;但是這樣一個標準化的方案同時意味著靈活度不夠。當需要劃分feature module&#xff0c;需要客制化組件的時候&#xff0c;這個方案的實施的成本就會遠遠超過預期&#xff0c;因此在項目中…

Flsak項目--圖片驗證碼

0. 圖片驗證碼的使用流程 2.后端接口編寫 verify_code.py中編寫接口代碼&#xff1a; # coding:utf-8from . import api from ihome.utils.captcha.captcha import captcha from ihome import redis_store, constants, db from flask import current_app, jsonify, make_respo…

數據庫與數據庫管理系統

數據庫是長期存儲在計算機內有組織的大量的共享的數據集合。可以供各種用戶共享&#xff0c;具有最小冗余度和較高的數據獨立性。數據庫管理系統在數據庫建立、運用和維護時對數據庫進行統一控制&#xff0c;以保證數據的完整性、安全性&#xff0c;并在多用戶同時使用數據庫時…

如何提高團隊情商

在公司發展中&#xff0c;總裁&#xff0c;總監&#xff0c;經理&#xff0c;項目經理&#xff0c;他們對團隊的建設意義重大&#xff0c;工作很重要&#xff0c;但團隊的情商才更重要&#xff0c;筆者公司的一個團隊&#xff0c;三十多個人就像一個人&#xff0c;命令所到之處…

ubuntu java classpath 設置_在Ubuntu中正確設置java classpath和java_home

我有錯誤Exception in thread"main" java.lang.NoClassDefFoundError:當我嘗試在Ubuntu上運行編譯類時。我使用的是一個非常簡單的helloworld示例&#xff0c;互聯網上已有數百萬的響應表明我的classpath和java_home變量設置錯誤。但是&#xff0c;我已經將etc/envir…

Polo the Penguin and Matrix

Little penguin Polo has an n??m matrix, consisting of integers. Lets index the matrix rows from 1 to n from top to bottom and lets index the columns from 1 to m from left to right. Lets represent the matrix element on the intersection of row i and column…

趣解 XSS和CSRF的原理

參考文章&#xff1a;趣解 XSS和CSRF的原理 推薦網站&#xff1a;古黑論 感謝作者分享&#xff01;

js異步解決方案 --- 回調函數 vs promise vs generater/yield vs async/await

javascript -- 深度解析異步解決方案 高級語言層出不窮, 然而唯 js 鶴立雞群, 這要說道js的設計理念, js天生為異步而生, 正如布道者樸靈在 node深入淺出--(有興趣的可以讀一下, 很有意思^_^) , 異步很早就存在于操作系統的底層, 意外的是&#xff0c;在絕大多數高級編程語言中…

什么是TPDU

TPDU,全稱Transport Protocol Data Unit&#xff0c;是指傳送協議數據單元。代表從一個傳輸實體發送至另一個傳輸實體的消息。 我們需要為傳輸實體之間交換的數據單元起一個更加一般化的名字&#xff0c;TCP的術語是數據段&#xff0c;它很容易混淆&#xff0c;而且在TCP領域之…

sql注入基本原理

1. 參考文獻&#xff1a; 趣解SQL注入原理 Sql注入基本原理 2.參考書籍

項目管理雜談-員工的積極性在哪里?

項目開發過程中&#xff0c;每每有人感嘆&#xff0c;曾幾何時&#xff0c;隊伍如何好帶&#xff0c;如何好用&#xff0c;而如今&#xff0c;人心繁雜&#xff0c;隊伍不好帶了。很多人的想法是“人望高處走”&#xff0c;不停的尋找待遇及其他方面更好的單位。其實&#xff0…

centos7硬盤分區

首先在虛擬機的設置中為系統添加硬盤 使用fdisk -l /dev/sdb 查看未分區的硬盤 fdisk -l /dev/sda 這是已經分區好得 接下來我們就要對sdb進行分區: 首先使用fdisk /dev/sdb 接著輸入m可以看到詳細命令 進行添加分區 已經建立好4個主分區&#xff0c;在建立時會看到以下 刪除…

java上傳rar文件_java實現上傳zip/rar壓縮文件,自動解壓

在pom中添加解壓jar依賴4.0.0org.springframework.bootspring-boot-starter-parent2.1.2.RELEASEcom.hfuncompress0.0.1-SNAPSHOTuncompress上傳壓縮文件(rar或者zip格式),解壓1.8org.springframework.bootspring-boot-starter-weborg.projectlomboklomboktrueorg.springframew…

從MapReduce的執行來看如何優化MaxCompute(原ODPS) SQL

摘要&#xff1a; SQL基礎有這些操作&#xff08;按照執行順序來排列&#xff09;&#xff1a; from join(left join, right join, inner join, outer join ,semi join) where group by select sum distinct count order by 如果我們能理解mapreduce是怎么實現這些SQL中的基本操…

套接字(socket)基本知識與工作原理

套接字&#xff08;socket&#xff09;基本知識與工作原理 一、Socket相關概念 Socket通常也稱作“套接字”&#xff0c;用于描述IP地址和端口&#xff0c;是一個通信鏈的句柄。&#xff08;其實就是兩個程序通信用的。&#xff09; SOCKET用于在兩個基于TCP/IP協議的應用程序之…

python 多線程--重點知識

1.全局變量global的用法 2.多線程共享全局變量-args參數 注意args參數類型為元組&#xff0c;逗號不能少&#xff01;

Flask WTForm表單的使用

運行環境&#xff1a; python2.7 flask 0.11 flask-wtf 0.14.2 wtform能夠通過一個類定義一些字段&#xff0c;這些字段會在前端生成標簽&#xff0c;并且通過設置字段的驗證規則&#xff0c;自動判斷前端輸入數據的格式。 一般用于用戶登錄&#xff0c;用戶注冊等信息錄入。…