1.要求。
1.2IO分类
2.文件路径的小坑
- 在代码写相对路径,是相对java项目项目。
- 如无论是java文件中哪里写了。"java.txt"这种相对文件都是相对java项目,在项目下。如果是新建文件,还要刷新项目。
- 我的错误理解,是相对写的java文件旁。(错误)
3.复制文件案例
该方法传两个文件名,a复制一个文件b的功能
public void iocopy(String a,String b) {
File file1=new File(a);
File file2=new File(b);
try {
FileInputStream fileInputStream=new FileInputStream(file1);
FileOutputStream fileOutputStream=new FileOutputStream(file2);
int size=(int)file1.length();
byte[] buf=new byte[size];
fileInputStream.read(buf);
fileOutputStream.write(buf);
fileInputStream.close();
fileOutputStream.close();
System.out.println(file2.length());
System.out.println(file2.getAbsolutePath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4.随机流案例,文件分多部分拿内容。
-
思想:文件可以分三部分,找到个部分的指针
-
求文件长度可以分好。如len1,len2,len3
-
String line = in_and_out.readLine();
byte b[] = line.getBytes(“iso-8859-1”);
String content = new String(b);
System.out.println(content);
解决乱码的问题。。 -
in_and_out.seek(0);指向开始位置。
public void ioEmployeeout() {
File file=new File(“employee.txt”);
try {
RandomAccessFile in_and_out = new RandomAccessFile(file,“rw”);
int len1=0;
int len3=(int) file.length();
int len2=len3/3;
len3=len3/3*2;
try {
in_and_out.seek(0);
String line = in_and_out.readLine();
byte b[] = line.getBytes("iso-8859-1");
String content = new String(b);
System.out.println(content);
in_and_out.seek(len2);
line = in_and_out.readLine();
byte b1[] = line.getBytes("iso-8859-1");
content = new String(b1);
System.out.println(content);
in_and_out.seek(len3);
line = in_and_out.readLine();
byte b2[] = line.getBytes("iso-8859-1");
content = new String(b2);
System.out.println(content);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
5.对象的序列化
Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程。
- 大概印象:一般用于服务器之间。
- 实现了Serializable或Externalizable接口的类的对象才能被序列化,
- 如:clas student extend Serializable 就实现了序列化。
- 一方面,发送方需要把这个Java对象转换为字节序列,然后在网络上传送;另一方面,接收方需要从字节序列中恢复出Java对象。
- 总结:
1)Java序列化就是把对象转换成字节序列,而Java反序列化就是把字节序列还原成Java对象。
2)采用Java序列化与反序列化技术,一是可以实现数据的持久化,在MVC模式中很是有用;二是可以对象数据的远程通信。
6.文件中读出和写入对象信息
6.1写入信息到文件中
- 功能:对象信息保存在文件中。
- 思路:把对象给方法参数。保存在文件中
- 技术:FileOutputStream字节输出流
- 实现:对象各个信息用“,”分割,组成字符串,保存在文件中
public void ioEmployeein2(Employee employee) {
//文件输出流
FileOutputStream fos = null;
try {
//写文件,覆盖
//fos = new FileOutputStream(“C:/Users/admin/workspace2/class16/src/com/softjx/abc.txt”);
//写文件,追加
fos = new FileOutputStream(“employee.txt”,true);
// fos = new FileOutputStream(“src/com//pctc/dao/impl/employee.txt”,true);
// fos = new FileOutputStream(“abc.txt”,true);
System.out.println(fos);
DateString dateString=new DateString();
String dateString2=dateString.string2Date(new Date());
String rString=employee.getName()+","+employee.getAge()+","+employee.getSex()+","+dateString2+"\n";
System.out.println(rString);
//把字符串转换字节数组
byte b[] = rString.getBytes();
fos.write(b);
//fos.write(b,3,10);
fos.flush();
fos.close();
System.out.println("写入完毕!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
6.2读出文件中的信息
-
功能:文件中读出对象数据
-
思路,调用方法,得到文件对应的文件信息。
-
技术:BufferedReader 字符缓冲输入流。
-
实现:一行读出。然后split分割。
-
分别读入对象,用集合保存对象。
public List ioEmployeein() {
FileReader fr=null;
BufferedReader br=null;
List lists=new ArrayList();
try {
//创建一个FileReader对象
fr=new FileReader(“employee.txt”);
//创建一个BufferedReader 对象
br=new BufferedReader(fr);
//读取一行数据
String line=br.readLine();
while(line!=null){
System.out.println(line);
String[] strs=line.split(",");
Employee employee1 = new Employee();
employee1.setName(strs[0]);
employee1.setAge(Integer.parseInt(strs[1]));//Integer.valueof(s)
employee1.setSex(strs[2]);
lists.add(employee1);
// employee.setbirthday(strs[3]);
line=br.readLine();
}
}catch(IOException e){
System.out.println("文件不存在!");
}finally{
try {
//关闭 流
if(br!=null)
br.close();
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lists;
}
6.3小结
这个两个案例是用字节输出流和字符缓冲输入流来实现功能。
对应相反的类也可以实现改功能。
-
扩展,这里是对象信息,完整代码,是进行和数据库的信息交互。
-
使用参数是对象信息,使用的时候,写入:就查询数据库,读出:
-
就保存在数据库中。
-
7.java访问exe文件。
-
## 7.1代码操作
-
public static void main(String[] args) {
try { Runtime ce = Runtime.getRuntime(); //File file = new File("C:/WINDOWS", "notepad.exe"); //D:\Program Files (x86)\Tencent\QQ\Bin\QQScLauncher.exe File file = new File("D:/Program Files (x86)/Tencent/QQ/Bin", "QQScLauncher.exe");//打开QQ Process pro=ce.exec(file.getAbsolutePath()); //Process pro= ce.exec(file.toString()); System.out.println(ce.maxMemory()); System.out.println(ce.totalMemory()); } catch (Exception e) { }
}