forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceLoadDemo.java
More file actions
53 lines (51 loc) · 1.51 KB
/
Copy pathResourceLoadDemo.java
File metadata and controls
53 lines (51 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class ResourceLoadDemo{
private static final String RESOURCE_FILE = "resource.txt";
public static void main(String[] args) {
System.out.println(ResourceLoadDemo.class.getClassLoader());
InputStream is = ResourceLoadDemo.class.getResourceAsStream(RESOURCE_FILE);
if(is==null){
System.err.printf("%s not found%n", RESOURCE_FILE);
return ;
}
try{
byte[] unit = new byte[1024];
int rd, length = 0;
if(is.markSupported()){
System.out.println("markSupported is true.");
is.mark(Integer.MAX_VALUE);
while((rd=is.read())!=-1){
length+=rd;
}
byte[] image = new byte[length];
int i=0;
is.reset();
while((rd=is.read())!=-1){
image[i++] = (byte)rd;
}
for(i=0;i<16;i++){
System.out.printf("%02X ", image[i]);
}
}else{
System.out.println("markSupported is false, need alternative approach:)");
while((rd=is.read())!=-1){
length+=rd;
}
byte[] image = new byte[length];
// Obtain a new InputStream to read.
InputStream nis = ResourceLoadDemo.class.getResourceAsStream(RESOURCE_FILE);
int i =0;
while((rd=nis.read())!=-1){
image[i++] = (byte)rd;
}
for(i=0;i<16;i++){
System.out.printf("%02X ", image[i]);
}
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}