
时间: 2019年1月18日

本文向您介绍Java文件输入和输出流文件读写文件,主要包括Java文件输入和输出流文件读写文件的使用实例file读写文件,应用技巧,基本知识要点和注意事项的概述,具有一定的参考值,有需要的朋友可以参考.

如果制作jar包,则可以使用此方法file读写文件,这样可以获取类的绝对路径

//取得该类的绝对路径
String location = 类名.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//以下是获取jar包的上一级文件夹
int startIndex = 0;
if( endIndex == -1 ) {
endIndex = location.lastIndexOf("classes");
}
文件夹路径 = location.substring(startIndex, endIndex);

使用反射反射不是罐子
String path = class1.getClassLoader().getResource("").toString();
//file:/在这里这么做是为了消除开头的file://
int m = path.indexOf("/");
path = path.substring(m + 1);// 真正的路径
//如果文件不存在则会抛异常
public static void main(String[] arg0){
// 文件名称
logFileName = "-" + "2019-01-18" + ".txt";
File f = new File(路径名称);
String s="";
InputStream in=null;
try{
in=new FileInputStream(f);
//在这里设置每次读取多少个字符,不用特别大
byte[] b = new byte[10];
while(in.read(b)!=-1){
s+=new String(b);
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("content:"+s);
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-249395-1.html
中华士魄
再个地方政府保护当地企业也很正常
做法也多没见你列为顶级美食