为了账号安全,请及时绑定邮箱和手机立即绑定

文件传输基础——Java IO流

难度入门
时长 2小时 0分
学习人数
综合评分9.67
669人评价 查看评价
9.9 内容实用
9.6 简洁易懂
9.5 逻辑清晰
  • 一、 /** * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式 * @param fileName * @throws IOException */ public static void printHexByByteArray(String fileName)throws IOException{ FileInputStream in = new FileInputStream(fileName); byte[] buf = new byte[8 * 1024]; /*从in中批量读取字节,放入到buf这个字节数组中, * 从第0个位置开始放,最多放buf.length个 * 返回的是读到的字节的个数 */ int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大 int j = 1; for(int i = 0; i < bytes;i++){ System.out.print(Integer.toHexString(buf[i] & 0xff)+" "); if(j++%10==0){ System.out.println(); } }
    查看全部
  • 5)FileInputStream--->具体实现了在文件上读取数据 /** * 读取指定文件内容,按照16进制输出到控制台 * 并且每输出10个byte换行 * @param fileName * 单字节读取不适合大文件,大文件效率很低 */ public static void printHex(String fileName)throws IOException{ //把文件作为字节流进行读操作 FileInputStream in = new FileInputStream(fileName); int b ; int i = 1; while((b = in.read())!=-1){ if(b <= 0xf){ //单位数前面补0 System.out.print("0"); } System.out.print(Integer.toHexString(b)+" "); if(i++%10==0){ System.out.println(); } } in.close(); }
    查看全部
  • 4)输出流基本方法 out.write(int b) 写出一个byte到流,b的低8位 out.write(byte[] buf)将buf字节数组都写入到流 out.write(byte[] buf,int start,int size)
    查看全部
  • IO流(输入流、输出流) 字节流、字符流 1.字节流 1)InputStream、OutputStream InputStream抽象了应用程序读取数据的方式 OutputStream抽象了应用程序写出数据的方式 2)EOF = End 读到-1就读到结尾 3)输入流基本方法 int b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF in.read(byte[] buf) in.read(byte[] buf,int start,int size)
    查看全部
  • 对子类对象进行反序列化操作时,如果父类没有实现序列化接口,那么其父类的构造函数会被调用
    查看全部
  • public static void main(String[] args) throws IOException { File demo = new File("demo"); if (!demo.exists()) demo.mkdir(); File file = new File(demo, "raf.dat"); if (!file.exists()) file.createNewFile(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 指针的位置 System.out.println(raf.getFilePointer()); raf.write('A');// 只写了一个字节,並非整個char字符 System.out.println(raf.getFilePointer()); raf.write('B'); int i = 0x7fffffff; // 用write方法每次只能写一个字节,如果要把i写进去就得写4次 raf.write(i >>> 24);// 高8位 raf.write(i >>> 16); raf.write(i >>> 8); raf.write(i); System.out.println(raf.getFilePointer()); // 可以直接写一个int raf.writeInt(i); String s = "中"; byte[] gbk = s.getBytes("gbk"); raf.write(gbk); System.out.println(raf.length()); // 读文件,必须把指针移到头部 raf.seek(0); // 一次性读取,把文件中的内容都读到字节数组中 byte[] buf = new byte[(int) raf.length()];// raf.length()返回一個long型 raf.read(buf); System.out.println(Arrays.toString(buf)); for (byte b : buf) { System.out.println(Integer.toHexString(b & 0xff) + " "); } raf.close(); }
    查看全部
  • RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件。 RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型 在硬盘上的文件是byte byte byte存储的,是数据的集合 (2)打开文件 有两种模式"rw"(读写) "r"(只读) RandomAccessFile raf = new RandomeAccessFile(file,"rw") 文件指针,打开文件时指针在开头 pointer = 0; (3) 写方法 raf.write(int)--->只写一个字节(后8位),同时指针指向下一个位置,准备再次写入 (4)读方法 int b = raf.read()--->读一个字节 (5)文件读写完成以后一定要关闭(Oracle官方说明)
    查看全部
  • public static void main(String[] args) { // 了解构造函数的情况 查帮助 File file = new File("E:\\javaio\\imooc"); //System.out.println(file.exists()); if(!file.exists()) file.mkdir(); //創建文件夾 else file.delete(); //是否是一个目录 如果是目录返回true,如果不是目录or目录不存在返回的是false System.out.println(file.isDirectory()); //是否是一个文件 System.out.println(file.isFile()); //File file2 = new File("e:\\javaio\\日记1.txt"); File file2 = new File("e:\\javaio","日记1.txt"); if(!file2.exists()) try { file2.createNewFile();//創建一個文件 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } else file2.delete(); //常用的File对象的API System.out.println(file);//file.toString()的内容 System.out.println(file.getAbsolutePath()); System.out.println(file.getName()); System.out.println(file2.getName()); System.out.println(file.getParent()); System.out.println(file2.getParent()); System.out.println(file.getParentFile().getAbsolutePath()); }
    查看全部
  • 1.GBK編碼,中文2個字節,英文一個字節 2.UTF-8編碼,中文3個字節,英文一個字節 3.JAVA是雙字節編碼(UTF-16be),中文2個字節,英文2個字節
    查看全部
    0 采集 收起 来源:文件的编码

    2015-01-09

  • encodeDemo.java
    查看全部
    0 采集 收起 来源:文件的编码

    2014-11-11

  • 課程目標
    查看全部
    0 采集 收起 来源:文件的编码

    2014-11-11

  • 单字节读取不适合读取较大的文件,大文件效率很低 批量字节读取对大文件而言效率高
    查看全部
  • 字节流处理
    查看全部
  • 对子类对象进行反序列化操作时,若果其父类没有实现序列化接口,那么其父类的构造函数会被调用
    查看全部
  • 自己完成反序列化的操作
    查看全部

举报

0/150
提交
取消
课程须知
亲,为了更好的学习本门课程,需要您对二进制的知识有所了解,还要熟悉Java的基本语法和面向对象的知识。
老师告诉你能学到什么?
1、了解文件编码。 2、能够对文件和目录进行管理操作。 3、能够应用字节流和字符流对文件进行读写操作。 4、能够对对象进行序列化和反序列化。

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!