📄 listing15-07_resourcereader.java_ok
字号:
// Listing 15-7. Reading a Resource Correctly Using Some Defensive Techniques
public class ResourceReader {
public byte[] readResource( String url )
throws IOException
{
InputStream in = getClass().getResourceAsStream( url );
byte[] buffer;
// the following always returns 0 on Symbian and Motorola:
int available = in.available();
if ( available == 0 ) {
available = 4 * 1024; // 4 kb
}
buffer = new byte[ available ];
ByteArrayOutputStream out = new ByteArrayOutputStream( available );
int read;
try {
while ( (read = in.read( buffer ) ) != -1 ) {
out.write( buffer, 0, read );
}
} finally {
in.close();
}
return out.toByteArray();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -