案例:
public class Bianma { public static void main(String[] args) throws Exception { String s = "你好世界!"; byte[] bytes1 = s.getBytes(); for (byte b : bytes1) { System.out.println(Integer.toHexString(b & 0xff) + " "); } System.out.println(); byte[] bytes2 = s.getBytes("utf-8"); for (byte b : bytes2) { System.out.println(Integer.toHexString(b & 0xff) + " "); } System.out.println(); byte[] bytes3 = s.getBytes("gbk"); for (byte b : bytes3) { System.out.println(Integer.toHexString(b & 0xff) + " "); } } }
public class FileName { public static void main(String[] args) { File file = new File("E:\\javaio"); // System.out.println(file.exists()); if(!file.exists()){ file.mkdir(); }else{ file.delete(); } //是否是一个目录 System.out.println(file.isDirectory()); //是否是一个文件 System.out.println(file.isFile()); File file2 = new File("E:\\javaio\\riji.txt"); if(!file2.exists()){ try { file2.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }else{ file.delete(); } System.out.println(file); System.out.println(file.getAbsolutePath()); System.out.println(file.getName()); System.out.println(file2.getName()); System.out.println(file.getParent()); System.out.println(); } }
public class FileUtils { public static void listDirectory(File dir)throws IOException{ if(!dir.exists()){ throw new IllegalArgumentException("目录:" + dir + "不存在."); }if(!dir.isDirectory()){ throw new IllegalArgumentException(dir + "不是目录"); } String[] filenames = dir.list(); for (String string : filenames) { System.out.println(dir + "\\:" + string); } } }
public class FileUtilsTest1 { public static void main(String[] args) throws IOException { FileUtils.listDirectory(new File("E:\\HN")); } }RandomAccessFile java提供的对文件内容的访问,即可以读文件,也可以写文件。
public class RafDemo { /** * @param args */ public static void main(String[] args) throws IOException{ File demo = new File("E:\\demo"); if(!demo.exists()) demo.mkdir(); File file = new File(demo,"raf.dat"); if(!file.exists()) file.createNewFile(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.write('A'); raf.write('A'); System.out.println(raf.getFilePointer());//指针的位置 int i = 0x7fffffff; 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.read(buf); System.out.println(Arrays.toString(buf)); // String s1 = new String(buf); // System.out.println(s1); for (byte b : buf) { System.out.println(Integer.toHexString(b & 0xff) + " "); } raf.close(); } }IO流(输入流、输出流)
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class IO { 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){ System.out.print("0"); } System.out.print(Integer.toHexString(b) + " "); if(i++ % 10 == 0){ System.out.println(); } } in.close(); } public static void printHexByByteArray(String fileName) throws IOException{ FileInputStream in = new FileInputStream(fileName); byte[] buf = new byte[20 * 1024]; // int bytes = in.read(buf,0,buf.length); // int j = 0; // for(int i = 0;i < bytes; i++){ // if(buf[i] <= 0xf){ // System.out.print("0"); // } // System.out.print(Integer.toHexString(buf[i]) + " "); // j++; // if(j == 10){ // j=0; // System.out.println(); // } // } int bytes = 0; int j =0; while((bytes = in.read(buf,0,buf.length)) != -1){ for(int i = 0;i < bytes; i++){ System.out.print(Integer.toHexString(buf[i] & 0xff) + " "); j++; if(j == 10){ j=0; System.out.println(); } } } in.close(); } }
public class IOTest { /** * @param args */ public static void main(String[] args) { // try { // IO.printHex("E:\\javaio\\123.txt"); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // } try { IO.printHexByByteArray("E:\\javaio\\123.txt"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
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(); } 测试: try { IO.copyFile(new File("E:\\javaio\\123.txt"), new File("E:\\javaio\\234.txt")); System.out.println("success"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class DosDemo { /** * @param args * @throws IOException */ 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); dos.writeUTF("中国"); dos.writeChars("中国"); dos.close(); IO.printHex(file); } }
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class DisDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; IO.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(); } }
public static void copyFileByBuffer(File srcFile, File desFile) 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(desFile)); int c; while((c = bis.read()) != -1){ bos.write(c); bos.flush();//刷新缓冲区 } bis.close(); bos.close(); } public static void copyFileByByte(File srcFile,File desFile)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(desFile); int c; while((c = in.read()) != -1){ out.write(c); out.flush(); } in.close(); out.close(); } }
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class IsOsDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("E:\\javaio\\abc.txt"); InputStreamReader isr = new InputStreamReader(in);//默认项目的编码 FileOutputStream out = new FileOutputStream("E:\\javaio\\34.jpg"); OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8"); // int c; // while((c = isr.read()) != -1){ // System.out.print((char)c); // } /*方法二: * * */ char[] buffer = new char[8 * 1024]; int c; while((c = isr.read(buffer,0,buffer.length)) != -1){ String s = new String(buffer,0,c); System.out.print(s); osw.write(buffer,0,c); osw.flush(); } isr.close(); osw.close(); } }