package io流.路径;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class Test {
public static void main1(String[] args) throws IOException {
//路径一,当前项目的路径下(例如我的在D:\workspace\javaTest\1.txt)
InputStream fis = new FileInputStream("1.txt");
//路径二,当前项目的bin目录(可以说是src目录,但是不准确)
fis = Test.class.getClassLoader().getResourceAsStream("2.txt");
// int data;
// while((data=fis.read())!=-1) {
// System.out.print((char)data);
// }
// System.out.println("=========================");
//路径三,当前类这个文件夹
fis = Test.class.getResourceAsStream("3.txt");
int data;
while((data=fis.read())!=-1) {
System.out.print((char)data);
}
fis.close();
}
public static void main2(String[] args) throws IOException {
//1.向当前项目根目录的1.txt
FileOutputStream fos = new FileOutputStream("1.txt");
//2.向src目录写数据
String path =Test.class.getResource("/").getPath();
System.out.println(path);
//3.当前类路径所在目录
path = Test.class.getResource("").getPath();
path =URLDecoder.decode(path,"utf-8");
System.out.println(path);
fos = new FileOutputStream(path+"2.txt");
fos.write('b');
fos.close();
}
}
(Java基础)路径问题
最新推荐文章于 2023-05-23 21:25:20 发布