日报 18/06/25 26
io流
-  availablepublic?int?available()throws IOException 返回從該輸入流中可以讀取(或跳過)的字節數的估計值,而不會被下一次調用此輸入流的方法阻塞。 下一個調用可能是同一個線程或另一個線程。 這個多個字節的單個讀取或跳過將不會被阻塞,但可以讀取或跳過較少的字節。注意,盡管一些實現InputStream將在流中返回的字節總數,許多人不會。 使用此方法的返回值分配用于保存此流中的所有數據的緩沖區是絕對不正確的。 子類此方法的實現可以選擇拋出IOException如果輸入流已通過調用關閉close()方法。 該available類方法InputStream總是返回0 。 這個方法應該被子類覆蓋。 結果
- 當輸入流到達輸入流的末尾時,可以從該輸入流中讀取(或跳過)的字節數,而不阻塞的字節數 0 。異常
- IOException - 如果發生I / O錯誤。 ?
?注解
①如果要從網絡中下載文件時,我們知道網絡是不穩定的,也就是說網絡下載時,read()方法是阻塞的,說明這時我們用inputStream.available()獲取不到文件的總大小。此時就需要通過
HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
httpconn.getContentLength();//獲取文件長度
來獲取文件的大小。
②如果是本地文件的話,用此方法就返回實際文件的大小。
③這個方法其實是通過文件描述符獲取文件的總大小,而并不是事先將磁盤上的文件數據全部讀入流中,再獲取文件總大小。 中文名
import java.io.*;
/**
*使用BufferedReader讀取文件內容(字符流)
*/
public class BufferedReaderTest04 {
public static void main(String[] args) {
/*
* 創建輸入流 Reader
* BufferedReader和FileReader聯合使用
* 效率高 底層有默認的緩沖區 還可以逐行讀取
*/
//創建BufferedReader對象 傳遞Reader的對象
BufferedReader br=null;
Reader reader=null;
try {
reader = new FileReader("d:/hello.txt");
br=new BufferedReader(reader);
//一行一行的讀取
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//關閉流 先開的后關
try {
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?
package com.b3.unit3.c1;import java.io.*;
/**
* 使用BufferedWriter寫入文件(字符流)
*/
public class BufferedWriterTest06 {
public static void main(String[] args) {
try {
//創建輸出流對象
Writer writer=new FileWriter("d:/hello.txt",true);
//創建BufferedWriter對象
BufferedWriter bw=new BufferedWriter(writer);
//換行
bw.newLine();
bw.write("北京也愛你!");
bw.newLine();
bw.write("北京也愛你!");
bw.close();
writer.close();
//獲取輸入流
Reader reader=new FileReader("d:/hello.txt");
BufferedReader br=new BufferedReader(reader);
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?
package com.b3.unit3.c1;import java.io.*;
/**
* 讀取2進制文件
*/
public class DataInputStreamTest08 {
public static void main(String[] args) throws Exception {
//創建輸入流
InputStream fis = new FileInputStream("d:/mm.mp3");
//讀取2進制文件
DataInputStream dis = new DataInputStream(fis);
//復制文件到另一個目錄
OutputStream fos = new FileOutputStream("d:/U1/慢慢.mp3");
//以2進制的方式輸出到指定的目錄
DataOutputStream dos = new DataOutputStream(fos);
//開始讀取
int data;
while ((data = dis.read()) != -1) {
//寫入
dos.write(data);
}
dos.close();
fos.close();
dis.close();
fis.close();
}
}
?
package com.b3.unit3.c1;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 使用FileInputStream讀取文件內容(字節流)
*
* 所有的輸入流都有 讀 的方法
* 所有的輸出流都有 寫 的方法
*
* 輸入輸出流都是相對于計算機的內存而言
*
* 字節輸入流的基類是 InputStream
* 字節輸出流的基類是 OutputStream
*
* 字符輸入流的基類是 Reader
* 字符輸出流的基類是 Writer
*
*
* utf-8 :中文字符以及中文都是占3個字節! 數字和字母都是占1個!
* GBK: 中文字符以及中文都是占2個字節!
*/
public class FileInputStreamTest {
public static void main(String[] args) {
InputStream stream = null;
try {
stream = new FileInputStream("d:/hello.txt");
System.out.println("可讀取的字節數:" + stream.available());
int num = 0;
while ((num = stream.read()) != -1) {
// 出現中文亂碼 因為utf-8中中文占3個字節
System.out.println((char) num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉流
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?
package com.b3.unit3.c1;import java.io.FileReader;
import java.io.Reader;
/**
* .使用FileReader讀取文件內容(字符流)
*/
public class FileReaderTest03 {
public static void main(String[] args) throws Exception {
//獲取當前的編碼格式
System.out.println("使用的編碼為:"+System.getProperty("file.encoding"));
//創建輸入流 Reader
Reader reader=new FileReader("d:/hello.txt");
//因為讀取的字符 創建數據的中轉站 會有多余的空格產生
char [] words=new char[1024];
int num;
//需要字符串的 拼接
StringBuilder sb=new StringBuilder();
while((num=reader.read(words))!=-1){
sb.append(words);
}
System.out.println(sb.toString());
//關閉輸入流
reader.close();
}
}
package com.b3.unit3.c1;
import java.io.*;
/**
* 使用InputStreamReader解決中文亂碼問題
*/
public class InputStreamReaderTest07 {
public static void main(String[] args) {
BufferedReader br=null;
InputStreamReader isr=null;
InputStream stream=null;
try {
//創建輸入流對象
stream=new FileInputStream("d:/hello.txt");
System.out.println("文件的大小:"+stream.available());
//使用InputStreamReader來解決亂碼
isr=new InputStreamReader(stream, "utf-8");
//讀取
br=new BufferedReader(isr);
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?
package com.b3.unit3.c1;import java.io.File;
import java.io.IOException;
/**
* 使用File操作文件(字節流)
*/
public class IoTest {
public static void main(String[] args) throws IOException {
/* 01.刪除或者創建文件
* File file=new File("e:/io.txt");
addOrDel(file); */
File file = new File("d:/java/hello");
//file.mkdir(); 只能創建一層目錄
file.mkdirs(); //同時創建多層目錄
}
/**
* 刪除或者創建文件
*/
public static void addOrDel(File file) throws IOException {
if (!file.exists()) { //判斷文件是否存在
if (file.createNewFile()) {//創建成功
System.out.println("創建成功!");
System.out.println("是否是文件:" + file.isFile());
System.out.println("文件名稱:" + file.getName());
System.out.println("文件大小:" + file.length());
System.out.println("文件的絕對路徑:" + file.getAbsolutePath());
} else {
System.out.println("創建失敗");
}
} else {
System.out.println("文件已經存在!");
if (file.delete()) {//刪除文件
System.out.println("刪除成功!");
}
}
}
}
?
package com.b3.unit3.c1;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 使用OutputStream寫入文件內容(字節流)
*
* 01.java項目的編碼格式 要和輸出文件的編碼一致 都是UTF-8
* 02.如果系統中沒有指定的文件,會默認創建
* 03.如果重復輸出,則上次的內容會被覆蓋
* 如果不想覆蓋!使用重載!在第二個參數的位置輸入true
*/
public class OutputStreamTest {
public static void main(String[] args) {
OutputStream stream = null;
try {
// stream = new FileOutputStream("e:/hello.txt");
stream = new FileOutputStream("d:/hello.txt", true); // 在原本的內容上拼接
// 這里是整體作為一個參數
stream.write("中國1".getBytes());
stream.write("中國2".getBytes());
// 強行把緩沖區的數據寫到輸出流中
stream.flush();
stream.write("中國3".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?
package com.b3.unit3.c1;import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 使用OutputStreamWriter寫入文件(字符流)
*/
public class OutputStreamWriterTest05 {
public static void main(String[] args) {
try {
/*
* 輸出流 默認的會給我們創建新的文件
* 不使用第二個參數! 那么默認會覆蓋之前的內容
* ctrl +shift +t 選中需要查詢的類或者接口
*/
Writer writer=new FileWriter("d:/hello.txt",true);
writer.write("我愛北京天安門!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?
package com.b3.unit3.c1;import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**
* 序列化
*/
public class SerializableTest {
public static void main(String[] args) throws Exception { //序列化操作
//首先實例化一個對象
Student student=new Student(1, 500, "小白2");
//想序列化?從內存中 放入 持久化的介質中 輸出流
FileOutputStream fos=new FileOutputStream("d:/student.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//開始持久化
oos.writeObject(student);
oos.close();
fos.close();
}
}
?
package com.b3.unit3.c1;import java.io.Serializable;
/**
* 序列化和反序列化
*
* 序列化:將內存中對象的狀態或者信息 轉換成 持久化的過程!
* 反序列化:把持久化的對象 變成 內存中的一個對象的過程!
* 目的:
* 01.使自定義的對象 持久化!對象本身是在內存中的!我們想把它持久化!
* 02.把對象從一個地方傳遞到另一個地方!
* 03.使程序具有維護性!
*
* 怎么才能實現對象的序列化??
* 1.讓對象所屬的類 實現Serializable 接口 之后, 這個類 就可以序列化了!
* Serializable:只是一個能否被序列化的標記!
*/
public class Student implements Serializable { //實體類
private Integer id;
private Integer age;
private String name;
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
}
public Student() {
super();
}
public Student(Integer id, Integer age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} package com.b3.unit3.c1;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
/**
* 反序列化
* 需要和 序列化時候的包名 一致 不然 沒法反序列化
*/
public class StudentTest {
public static void main(String[] args) throws Exception {
// 從文件中把對象 拿到 內存中 輸入流
FileInputStream fis = new FileInputStream("d:/student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// 讀取文件中的對象
Student student = (Student) ois.readObject();
System.out.println(student.getId());
System.out.println(student.getAge());
System.out.println(student.getName());
ois.close();
fis.close();
}
}
?文件描述符
#include <stdio h="">
#include <stdlib h="">
#include <fcntl h="">
#include <sys types="" h="">
#include <unistd h="">
#include <sys types="" h="">
int main(void)
{
 int fd, pid, status;
 char buf[10];
 if ((fd = open("./test.txt", O_RDONLY)) < 0) {
 perror("open"); exit(-1);
 }
 if ((pid = fork()) < 0) {
 perror("fork"); exit(-1);
 } else if (pid == 0) { //child
 read(fd, buf, 2);
 write(STDOUT_FILENO, buf, 2);
 } else { //parent
 sleep(2);
 lseek(fd, SEEK_CUR, 1);
 read(fd, buf, 3);
 write(STDOUT_FILENO, buf, 3);
 write(STDOUT_FILENO, "\n", 1);
 }
 return 0;
 }
</sys></unistd></sys></fcntl></stdlib></stdio>
?
轉載于:https://www.cnblogs.com/yunfeioliver/p/9227968.html
總結
以上是生活随笔為你收集整理的日报 18/06/25 26的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: jquery显示和隐藏元素
- 下一篇: CLion之C++框架篇-安装工具,基础
