吕梁做网站的公司,上海网站建设中小型企业,可不可以异地建设网站,公司方案io流 available public int available()throws IOException 返回从该输入流中可以读取#xff08;或跳过#xff09;的字节数的估计值#xff0c;而不会被下一次调用此输入流的方法阻塞。 下一个调用可能是同一个线程或另一个线程。 这个多个字节的单个读取或跳过将不会被阻… io流 available public int available()throws IOException 返回从该输入流中可以读取或跳过的字节数的估计值而不会被下一次调用此输入流的方法阻塞。 下一个调用可能是同一个线程或另一个线程。 这个多个字节的单个读取或跳过将不会被阻塞但可以读取或跳过较少的字节。 注意尽管一些实现InputStream将在流中返回的字节总数许多人不会。 使用此方法的返回值分配用于保存此流中的所有数据的缓冲区是绝对不正确的。 子类此方法的实现可以选择抛出IOException如果输入流已通过调用关闭close()方法。 该available类方法InputStream总是返回0 。 这个方法应该被子类覆盖。 结果 当输入流到达输入流的末尾时可以从该输入流中读取或跳过的字节数而不阻塞的字节数 0 。异常 IOException - 如果发生I / O错误。 注解 ①如果要从网络中下载文件时我们知道网络是不稳定的也就是说网络下载时read()方法是阻塞的说明这时我们用inputStream.available()获取不到文件的总大小。此时就需要通过HttpURLConnection httpconn (HttpURLConnection)url.openConnection(); httpconn.getContentLength();//获取文件长度来获取文件的大小。②如果是本地文件的话用此方法就返回实际文件的大小。③这个方法其实是通过文件描述符获取文件的总大小而并不是事先将磁盘上的文件数据全部读入流中再获取文件总大小。 中文名文件描述符外文名file descriptor作 用内核利用文件描述符来访问文件形 式非负整数文件描述符在形式上是一个非负整数。实际上它是一个索引值指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时内核向进程返回一个文件描述符。在程序设计中一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于UNIX、Linux这样的操作系统 package com.b3.unit3.c1;import java.io.*;/** *使用BufferedReader读取文件内容字符流 */public class BufferedReaderTest04 { public static void main(String[] args) { /* * 创建输入流 Reader * BufferedReader和FileReader联合使用 * 效率高 底层有默认的缓冲区 还可以逐行读取 */ //创建BufferedReader对象 传递Reader的对象 BufferedReader brnull; Reader readernull; try { reader new FileReader(d:/hello.txt); brnew BufferedReader(reader); //一行一行的读取 String linenull; while((linebr.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 writernew FileWriter(d:/hello.txt,true); //创建BufferedWriter对象 BufferedWriter bwnew BufferedWriter(writer); //换行 bw.newLine(); bw.write(北京也爱你); bw.newLine(); bw.write(北京也爱你); bw.close(); writer.close(); //获取输入流 Reader readernew FileReader(d:/hello.txt); BufferedReader brnew BufferedReader(reader); String linenull; while((linebr.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 readernew FileReader(d:/hello.txt); //因为读取的字符 创建数据的中转站 会有多余的空格产生 char [] wordsnew char[1024]; int num; //需要字符串的 拼接 StringBuilder sbnew StringBuilder(); while((numreader.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 brnull; InputStreamReader isrnull; InputStream streamnull; try { //创建输入流对象 streamnew FileInputStream(d:/hello.txt); System.out.println(文件的大小stream.available()); //使用InputStreamReader来解决乱码 isrnew InputStreamReader(stream, utf-8); //读取 brnew BufferedReader(isr); String linenull; while((linebr.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 filenew 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 writernew 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 studentnew Student(1, 500, 小白2); //想序列化从内存中 放入 持久化的介质中 输出流 FileOutputStream fosnew FileOutputStream(d:/student.txt); ObjectOutputStream oosnew 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