输入/输出(一)——编码,文件类,随机读写文件类
来源:互联网 发布:koala mac版下载 编辑:程序博客网 时间:2024/06/09 08:32
不同编码方式总结
import java.io.UnsupportedEncodingException;public class Encode { public static void main(String[] args) throws UnsupportedEncodingException{ String s = "慕课ABC"; byte[] bytes1 = s.getBytes(); for(Byte b:bytes1){ System.out.print(b+" "); /*System.out.print(Integer.toHexString(b)); System.out.println();*/ } System.out.println(); byte[] bytes2 = s.getBytes("gbk");//GBK编码一个汉子占两个字节,一个字母占一个字节 for(Byte b:bytes2){ System.out.print(b+" "); } System.out.println(); byte[] bytes3 = s.getBytes("utf-8");//utf-8编码一个汉子占三个字节,一个字母占一个字节 for(Byte b:bytes3){ System.out.print(b+" "); } System.out.println(); byte[] bytes4 = s.getBytes("utf-16be");//utf-16be编码一个汉子占两个字节,一个字母也占两个字节 for(Byte b:bytes4){ //java采用的是此编码 System.out.print(b+" "); } System.out.println(); String s1 = new String(bytes4,"utf-16be"); System.out.println(s1); }}
文件类使用总结
import java.io.File;import java.io.IOException;/*File类用于表示文件(目录)的信息,名称啊,大小啊,是否存在啊等,不能访问文件里面的信息 * * */public class FileDemo { public static void main(String[] args){ //双斜杠和反斜杠都可以,File.separator = \ File file1 = new File("e:"+File.separator+"AndroidStudioProjects"+File.separator+"mooc"); System.out.println(file1.toString()); System.out.println(file1);//相当于file1.toString()的路径 System.out.println(file1.getAbsolutePath()); System.out.println(file1.getName()); System.out.println(file1.getParent());//``````````````````````````````````````````````````````````````````````````` //判断该文件夹是否存在 File file = new File("E:\\AndroidStudioProjects\\mooc/mm"); System.out.println(file.exists()); //如果不存在,就创建文件夹,如果存在就删除 if(!file.exists()){ //file.mkdir(); file.mkdirs();//创建多级目录 }else{ file.delete(); } System.out.println(file.exists());//``````````````````````````````````````````````````````````````````````````` //是否是一个目录,即是不是文件夹 File file2 = new File("E:\\AndroidStudioProjects\\nn.txt"); System.out.println(file2.exists()); System.out.println(file2.isDirectory());//是否是一个目录,即文件夹 System.out.println(file2.isFile());//是否是文件,即单个文件 System.out.println(file2.getParent()); System.out.println(file2.getParentFile().getName());//``````````````````````````````````````````````````````````````````````````` //判断文件是否存在, // File file3 = new File("E:\\AndroidStudioProjects\\日记.txt"); File file3 = new File("E:\\AndroidStudioProjects","日记.txt"); if(!file3.exists()){ try { file3.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else file3.delete(); }}
随机读写文件总结(RandomAccessFile)
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class RandomDemo {public static void main(String[] args) throws IOException{ File demo = new File("demo"); if(!demo.exists()){ demo.mkdir(); } File file = new File(demo,"raf.txt"); if(!file.exists()){ file.createNewFile(); } //使用RandomAccessFile进行读写操作 RandomAccessFile raf = new RandomAccessFile(file,"rw"); System.out.println(raf.getFilePointer());//指针位置是从头开始的 String s = "中"; byte[] bytes = s.getBytes("utf-8");//GBK编码一个汉子占两个字节,一个字母占一个字节 raf.write(bytes); raf.write('A'); System.out.println(raf.getFilePointer()); raf.write('B'); System.out.println(raf.getFilePointer()); byte[] b = "ABC".getBytes(); System.out.println(b.length); raf.write(b); System.out.println(raf.getFilePointer()); int i = 0x7fffffff;//java采用的是utf-16be编码方式,一个int型占四个字节,汉子占两个字节,字符型占一个字节//用write方法每次只能写一个字节,要吧i写进去,就要写四次 raf.write(i>>>24);//高8位 raf.write(i>>>16); raf.write(i>>>8); raf.write(i); System.out.println(raf.getFilePointer()); /*String s = "中"; byte[] bytes = s.getBytes("GBK");//GBK编码一个汉子占两个字节,一个字母占一个字节 raf.write(bytes);*/ System.out.println(raf.getFilePointer()); //读文件,必须把指针移到头部 raf.seek(0); byte[] byte1 = new byte[(int)raf.length()];//一次性读取 raf.read(byte1);}}
遍历文件夹总结
import java.io.File;public class Tools { /** * 遍历文件夹 * */ public static void listDirectory(File dir){ if(!dir.exists()){ throw new IllegalArgumentException("目录:"+dir+"不存在"); } if(!dir.isDirectory()){ throw new IllegalArgumentException(dir+"不是目录"); } /*String[] filenames = dir.list();//返回的是字符串数组,直接子目录的名字,不包含次子目录的名字 for(String filename:filenames){ System.out.println(dir+"\\"+filename); }*/ //如果要遍历子目录下的内容就需要构造File对象做递归,以下是返回的是File对象 File[] files = dir.listFiles();//返回的是直接子目录(文件)的抽象 if(files != null && files.length>0){ for(File file:files){ if(file.isDirectory()){ listDirectory(file); } System.out.println(file); } } } public static void main(String[] args){ File dir = new File("F:\\gct"); listDirectory(dir); }}
1 0
- 输入/输出(一)——编码,文件类,随机读写文件类
- 文件随机读写专用类——RandomAccessFile
- RandomAccessFile类实现随机读写文件
- RandomAccessFile类的使用,随机读写文件。
- RandomAccessFile随机读写文件操作类讲解
- 文本文件读写(1.5.1 文件输入和输出)?
- 输入/输出流---打开,读写文件操作
- R:读写文件(输入与输出)
- 一、C文件输入/输出
- 002_007 Python 随机输入/输出即随机读取文件
- [疯狂Java]I/O:文件随机读写专用类——RandomAccessFile
- Java IO操作——简单了解RandomAccessFile随机读写文件操作类的使用
- java文件随机读写
- 读写随机访问文件
- 随机读写文件RandomAccessFile
- hdfs随机读写文件
- C++ 文件随机读写
- 文件的随机读写
- 欢迎使用CSDN-markdown编辑器
- OpenCV实践之路——Python的安装和使用
- 最大子序列问题
- Dagger 笔记 一
- AFN
- 输入/输出(一)——编码,文件类,随机读写文件类
- 排序——冒泡排序(C++)
- android 中 checkBox 的使用
- 第四周项目 递归画图
- mysql 主从复制 主主配置
- 详细理解context(context访问资源的唯一性分析)
- MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列
- Problem F: 删出多余的空格
- FTP协议学习总结