P
pvii007
import java.io.*;
public class A
{
public static void main( String[] args)throws Exception
{
A app = new A();
BufferedReader bf = new BufferedReader(
new InputStreamReader(app.getResourceAsStream("a.txt"))
);
if( bf.ready())System.out.println(br.readLine());
}
}
a.txt
hello world
I compress the compiled A and a.txt in the jar file.
Run it. It's successful.
But
import java.io.*;
public class A
{
public static void main( String[] args)throws Exception
{
A app = new A();
BufferedReader bf = new BufferedReader(
new InputStreamReader(app.getResourceAsStream("/dir/a.txt"))// here I
modify it
);
if( bf.ready())System.out.println(br.readLine());
}
}
dir/a.txt
hello world
I still create the jar file.
Run it. It's failure
Why?
Is reading some file in the jar file approprate for the situation that the
file and the class is under the same package ?
If I want to use the class to read the file which is not under same package,
how to implement it?
public class A
{
public static void main( String[] args)throws Exception
{
A app = new A();
BufferedReader bf = new BufferedReader(
new InputStreamReader(app.getResourceAsStream("a.txt"))
);
if( bf.ready())System.out.println(br.readLine());
}
}
a.txt
hello world
I compress the compiled A and a.txt in the jar file.
Run it. It's successful.
But
import java.io.*;
public class A
{
public static void main( String[] args)throws Exception
{
A app = new A();
BufferedReader bf = new BufferedReader(
new InputStreamReader(app.getResourceAsStream("/dir/a.txt"))// here I
modify it
);
if( bf.ready())System.out.println(br.readLine());
}
}
dir/a.txt
hello world
I still create the jar file.
Run it. It's failure
Why?
Is reading some file in the jar file approprate for the situation that the
file and the class is under the same package ?
If I want to use the class to read the file which is not under same package,
how to implement it?