-
package com.imooc.io; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class IsrAndOsrDemo { public static void main(String[] args)throws IOException { FileInputStream in = new FileInputStream("e:\\javaio\\imooc.txt"); // InputStreamReader isr = new InputStreamReader(in,"utf-8"); InputStreamReader isr = new InputStreamReader(in); // 默认项目的编码 /*int c; while((c = isr.read())!=-1){ System.out.print((char)c); }*/ char[] buffer = new char[8*1024]; int c ; /* * 批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个 * 返回的是读到的字符的个数 */ while((c = isr.read(buffer,0,buffer.length))!=-1){ String s = new String(buffer,0,c); System.out.print(s); } } }查看全部
-
package com.imooc.io; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class IsrAndOsrDemo { public static void main(String[] args)throws IOException { FileInputStream in = new FileInputStream("e:\\javaio\\imooc.txt"); InputStreamReader isr = new InputStreamReader(in); int c; while((c = isr.read())!=-1){ System.out.print((char)c); } } }查看全部
-
2、字符流 1)编码问题 2)认识文本和文本文件 java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码) 文件时byte byte byte……的数据序列 文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果 3)字符流(Reader,Writer) 字符的处理,一次处理一个字符 字符的底层仍然是基本的字节序列 InputStreamReader 完成byte流解析为char流,按照编码解析 OutputStreamWriter 提供char流到byte流,按照编码处理查看全部
-
package com.imooc.io; import java.io.File; import java.io.IOException; public class IOUtilTest4 { public static void main(String[] args) { try { long start = System.currentTimeMillis(); // IOUtil.copyFileByBuffer(new File("e:\\滕玉龙\\FileUtils.java"), new File( // "e:\\滕玉龙\\QQ 1055212224.txt")); // 31" // IOUtil.copyFileByByte(new File("e:\\滕玉龙\\FileUtils.java"), new File(//单字节读取 // "e:\\滕玉龙\\敲代码使我快乐.txt")); // 51" // IOUtil.copyFileByByte(new File("e:\\滕玉龙\\月亮光光.mp3"), new File(//单字节读取 // "e:\\滕玉龙\\月亮光光-1.mp3")); //156786" // IOUtil.copyFileByBuffer(new File("e:\\滕玉龙\\月亮光光.mp3"), new File(//单字节读取 // "e:\\滕玉龙\\月亮光光-2.mp3")); // 缓冲读写 84938'' IOUtil.copyFile(new File("e:\\滕玉龙\\月亮光光.mp3"), new File(//单字节读取 "e:\\滕玉龙\\月亮光光-3.mp3")); // 缓冲读写 花时176毫秒 long end = System.currentTimeMillis(); System.out.println(end - start); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }查看全部
-
/* * 进行文件的拷贝,利用带缓冲的字节流 */ public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{ if(!srcFile.exists()){ throw new IllegalArgumentException("文件:" + srcFile + "不存在"); } if(!srcFile.isFile()){ throw new IllegalArgumentException(srcFile + "不是文件"); } BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int c ; while((c = bis.read())!=-1){ bos.write(c); bos.flush();// 刷新缓冲区 } bis.close(); bos.close(); }查看全部
-
package com.imooc.io; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class DisDemo { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub String file = "demo/dos.dat"; IOUtil.printHex(file); DataInputStream dis = new DataInputStream(new FileInputStream(file)); int i = dis.readInt(); System.out.println(i); i = dis.readInt(); System.out.println(i); long l = dis.readLong(); System.out.println(l); double d = dis.readDouble(); System.out.println(d); String s = dis.readUTF(); System.out.println(s); dis.close(); } } 运行结果: 0 0 0 0 0 0 0 a ff ff ff f6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a 40 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 e4 b8 ad e5 9b bd 4e 2d 56 fd 10 -10 10 10.5 中国查看全部
-
package com.imooc.io; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DosDemo { public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; DataOutputStream dos = new DataOutputStream(new FileOutputStream(file)); dos.writeInt(10); dos.writeInt(-10); dos.writeLong(10l); dos.writeDouble(10.5); // 采用utf-8编码写出 dos.writeUTF("中国"); // 采用utf-16be编码写出 dos.writeChars("中国"); dos.close(); IOUtil.printHex(file); } } 运行结果: 0 0 0 0 0 0 0 a ff ff ff f6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a 40 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 e4 b8 ad e5 9b bd 4e 2d 56 fd查看全部
-
package com.imooc.io; import java.io.File; import java.io.IOException; public class IOUtilTest3 { public static void main(String[] args) { // TODO Auto-generated method stub try { IOUtil.copyFile(new File("e:\\javaio\\imooc.txt"), new File( "e:\\javaio\\imooc1.txt")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }查看全部
-
package com.imooc.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOutDemo1 { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub //如果该文件不存在,则直接创建;如果存在,删除后创建 FileOutputStream out = new FileOutputStream("demo/out.dat",true); out.write('A');//写出了'A'的低八位 out.write('B');//写出了'B'的低八位 int a = 10;//write()方法只能写低八位,那么写一个int,需要写4次,每次8位。 out.write(a >>> 24); out.write(a >>> 16); out.write(a >>> 8); out.write(a); byte[] gbk = "中国".getBytes("gbk"); out.write(gbk); out.close(); IOUtil.printHex("demo/out.dat"); } }查看全部
-
public static void copyFile(File srcFile,File destFile)throws IOException{ if(!srcFile.exists()){ throw new IllegalArgumentException("文件:" + srcFile + "不存在"); } if(!srcFile.isFile()){ throw new IllegalArgumentException(srcFile + "不是文件"); } FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); byte[] buf = new byte[8 * 1024]; int b ; while((b = in.read(buf,0,buf.length))!=-1){ out.write(buf,0,b); out.flush(); // 最好加上 } in.close(); out.close(); }查看全部
-
package com.imooc.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOutDemo1 { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub //如果该文件不存在,则直接创建;如果存在,删除后创建 FileOutputStream out = new FileOutputStream("demo/out.dat",true); out.write('A');//写出了'A'的低八位 out.write('B');//写出了'B'的低八位 int a = 10;//write()方法只能写低八位,那么写一个int,需要写4次,每次8位。 out.write(a >>> 24); out.write(a >>> 16); out.write(a >>> 8); out.write(a); byte[] gbk = "中国".getBytes("gbk"); out.write(gbk); out.close(); IOUtil.printHex("demo/out.dat"); } } 运行结果: 41 42 0 0 0 0 0 0 0 a 41 42 0 0 0 0 0 0 0 a d6 d0 b9 fa 41 42 0 0 0 0 0 0 0 a d6 d0 b9 fa 41 42 0 0 0 0 0 0 0 a d6 d0 b9 fa查看全部
-
int bytes = 0; int j = 1 ; while((bytes = in.read(buf,0,buf.length))!=-1){ //for(int i = 0 ;i < buf.length ; i++){ for(int i = 0 ;i < bytes ; i++){ //有效字节的个数 System.out.print(Integer.toHexString(buf[i] & 0xff) + " "); if(j++%10==0){ System.out.println(); } } } 运行结果: d3 c3 c0 b4 d7 a2 b2 e1 b5 c4 b9 a4 be df查看全部
-
package com.imooc.io; import java.io.IOException; public class IOUtilTest2 { public static void main(String[] args) { // TODO Auto-generated method stub\ try { IOUtil.printHexByByteArray("e:\\java\\说明.txt"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 运行结果: 0ffffffd3 0ffffffc3 0ffffffc0 0ffffffb4 0ffffffd7 0ffffffa2 0ffffffb2 0ffffffe1 0ffffffb5 0ffffffc4 0ffffffb9 0ffffffa4 0ffffffbe 0ffffffdf查看全部
-
package com.imooc.io; import java.io.FileInputStream; import java.io.IOException; public class IOUtilTest1 { public static void main(String[] args) { try { IOUtil.printHex("e:\\java\\说明.txt"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 运行结果: d3 c3 c0 b4 d7 a2 b2 e1 b5 c4 b9 a4 be df查看全部
-
package com.imooc.io; import java.io.FileInputStream; import java.io.IOException;查看全部
举报
0/150
提交
取消