lesson7.1,7.2File类和RandomAccessFile类
来源:互联网 发布:java调用软件api接口 编辑:程序博客网 时间:2024/06/06 19:25
File类
封装文件本身信息,定义操作文件的方法
常用方法:
File(String pathname)
delete()
creatNewFile()
exists()
getName()
习题:判断某文件,存在则删除,不存在则创建
import java.io.File;public class TestFile {public static void main(String [] args){File f = new File("1.txt");System.out.println(f.canRead());System.out.println(f.exists());System.out.println(f.getAbsolutePath());System.out.println(f.getName());System.out.println(f.getParent());System.out.println(f.getPath());System.out.println(f.length());System.out.println(f.toString());if(f.exists()){f.delete();}else{try{f.createNewFile();}catch(Exception e){}}System.out.println(f.canRead());System.out.println(f.exists());System.out.println(f.getAbsolutePath());System.out.println(f.getName());System.out.println(f.getParent());System.out.println(f.getPath());System.out.println(f.length());System.out.println(f.toString());}}
RandomAccessFile类
仅限于操作文件,支持随机访问,擅长随机读写等长记录格式文件。
常用方法:
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
writeChars(String s)按字符序列将一个字符串写入该文件
writeInt(int v)写入整数
readChar()从文件读取一个字符
readInt()从文件读取32位整数
seek(long pos)跳转到pos的位置
skipBytes(ing n)向后跳n个字节
习题:存三名员工信息,先打印第二个人在打印第一个人第三个人
import java.io.RandomAccessFile;public class TestRandomFile {public static void main(String [] args) {RandomAccessFile f = null;try{f = new RandomAccessFile("staffInfo.txt", "rw"); }catch (Exception e){e.printStackTrace();}Staff s1 = new Staff("zhangsan", 20);Staff s2 = new Staff("lisi", 21);Staff s3 = new Staff("wangwu", 122);try{f.writeChars(s1.name);f.writeInt(s1.age);f.writeChars(s2.name);f.writeInt(s2.age);f.writeChars(s3.name);//写入字符串f.writeInt(s3.age);//写入整数//取第二个人f.seek(Staff.LEN*2+4);//跳到第二个人,unicode编码一个字符站两个字节,所以是8*2+4String strName1 = "";for(int i=0; i<Staff.LEN; i++){strName1 += f.readChar();}System.out.println(strName1+":"+f.readInt());//取第一个人f.seek(0);strName1 = "";for(int i=0; i<Staff.LEN; i++){strName1 += f.readChar();}System.out.println(strName1+":"+f.readInt());//取第三个人f.skipBytes(Staff.LEN*2+4);strName1 = "";for(int i=0; i<Staff.LEN; i++){strName1 += f.readChar();}System.out.println(strName1+":"+f.readInt());f.close();//关闭文件}catch(Exception e){e.printStackTrace();}}}class Staff{String name = null;int age;public static final int LEN = 8;//姓名给8个字符的长度Staff(String name, int age){if(name.length() > LEN)//多退少补{name = name.substring(0, 8);}else{while(name.length() < LEN){name += "\u0000";}}this.name = name;this.age = age;}}
阅读全文
0 0
- lesson7.1,7.2File类和RandomAccessFile类
- File类和RandomAccessFile类
- lesson7.5FileInpuStreamt和FileWriter类
- I/O编程-File类和RandomAccessFile类
- File类的学习和RandomAccessFile的学习
- Java学习笔记(1)--File类、RandomAccessFile类
- File类与RandomAccessFile类范例
- 10、J2SE基础-File类、RandomAccessFile类
- 12. JAVA IO Party 1 (目标、File类、RandomAccessFile类、字节流和字符流、转换流) ----- 学习笔记
- RandomAccessFile和File的区别
- 文件操作File和RandomAccessFile
- 7.2 RandomAccessFile类
- 输入和输出--RandomAccessFile类
- Java中IO的相关操作——File类和RandomAccessFile类
- RandomAccessFile类
- RandomAccessFile类
- RandomAccessFile类
- RandomAccessFile类
- python 安装dlib和boost
- 3sum和4sum(two pointers)
- oj 对称三位数素数
- 约瑟夫环
- java notify,wait,notifyAll理解和实例(一)
- lesson7.1,7.2File类和RandomAccessFile类
- node js 安装 测试
- 343. Integer Break
- java类方法继承为什么其修饰词的范围不能越来越小
- 404 page容错页面
- QT5-msvc2015 实例之计算器
- url 中提取数据
- mysql 常用sql语句
- ToolBar+Listview+EditText,类似微信聊天页面软键盘弹出问题