首页 技术 正文
技术 2022年11月15日
0 收藏 847 点赞 4,822 浏览 2747 个字

在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

Separator:

比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File (“C:\tmp\test.txt”);
在Linux下则是这样的:
File file2 = new File (“/tmp/test.txt”);

如果要考虑跨平台,则最好是这么写:
File myFile = new File(“C:” + File.separator + “tmp” + File.separator, “test.txt”);

File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。

package cn.mndl;
import java.io.File ;
import java.io.IOException ;public class Hello { public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File ("E:" + File.separator + "test.txt") ;
if(f.exists()){
f.delete() ;
System.out.println("删除成功") ;
} else {
try{
f.createNewFile() ;
System.out.println("name of teh currect file :" + f.getName()) ;
System.out.println("parents director of the currect file :" + f.getParent()) ;
System.out.println("full path of currect file :" +f.getPath()) ;
System.out.println("is readable ? :"+f.canRead());
System.out.println("is writable ?" + f.canWrite());
} catch (IOException e){
e.printStackTrace(); }
}
}}
package cn.mndl;
import java.io.BufferedReader;
import java.io.File ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException ;public class Hello { public static void main(String[] args) {
// TODO Auto-generated method stub
try{
//第一步,使用FILE类定义一个文件
File inFile = new File("in.txt") ;
File outFile = new File("out.txt") ;
//第二步,用一个字节流或者字符流的子类进行流对象 的实例化
FileReader fi = new FileReader(inFile) ;
BufferedReader bfi = new BufferedReader(fi) ;
FileWriter fo = new FileWriter(outFile);
//第三步,读写操作
String l = "" ;
String [] arrs = null ;
while((l = bfi.readLine())!= null){
arrs = l.split(",") ;
System.out.println(arrs[]);
}
//第四步,关闭字节或者字节流
fi.close();
bfi.close();
fo.close();
} catch (IOException e){
e.printStackTrace() ;
} }}

对象流

package cn.mndl;
import java.io.BufferedReader;
import java.io.File ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException ;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;class Student implements Serializable {
private int id ;
private String name ;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public String toString(){
return "id: " + this.id + ", name : " + this.name;
}}
public class Hello { public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Student stu = new Student(,"zhuopeng") ;
Student s ;
try{
FileOutputStream out = new FileOutputStream("123.txt") ;//输出到文件
ObjectOutputStream so = new ObjectOutputStream(out) ;
so.writeObject(stu) ;
so.close();
out.close(); FileInputStream in = new FileInputStream("123.txt") ;//从文件中读入
ObjectInputStream io = new ObjectInputStream(in) ;
s = (Student) io.readObject() ;
System.out.println(s) ; } catch(FileNotFoundException e){
e.printStackTrace() ;
} catch(Exception e){
e.printStackTrace();
}
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,154
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,623
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,465
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,239
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,042