y****n 发帖数: 192 | 1 如何从一个文件读入数据,得到的是bytes,而不是字符串。请大家指教,谢谢! |
A**o 发帖数: 1550 | 2 FileInputStream
【在 y****n 的大作中提到】 : 如何从一个文件读入数据,得到的是bytes,而不是字符串。请大家指教,谢谢!
|
y****n 发帖数: 192 | 3 public static byte[] getRawByteDataFromFile(){
StringBuffer sb = new StringBuffer();
String s = null;
byte[] rawByteData = null;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(rawDataFile));
while((s=in.readLine())!=null){
sb.append(s+"\n");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTra
【在 A**o 的大作中提到】 : FileInputStream
|
h*****0 发帖数: 4889 | 4 前面说了FileInputStream...
真奇怪你明明已经都会了,为啥为不知道FileInputStream这么简单的东西?别的程序
员改行过来的?
【在 y****n 的大作中提到】 : public static byte[] getRawByteDataFromFile(){ : StringBuffer sb = new StringBuffer(); : String s = null; : byte[] rawByteData = null; : BufferedReader in = null; : try { : in = new BufferedReader(new FileReader(rawDataFile)); : while((s=in.readLine())!=null){ : sb.append(s+"\n"); : }
|
y****n 发帖数: 192 | 5 我知道FileInputStream, 但它的read()方法质只读进一个byte,我想得到所有数据
的byte[]
【在 h*****0 的大作中提到】 : 前面说了FileInputStream... : 真奇怪你明明已经都会了,为啥为不知道FileInputStream这么简单的东西?别的程序 : 员改行过来的?
|
h*****0 发帖数: 4889 | 6 ...
有read(byte[])啊
【在 y****n 的大作中提到】 : 我知道FileInputStream, 但它的read()方法质只读进一个byte,我想得到所有数据 : 的byte[]
|
g*****g 发帖数: 34805 | 7 FileInputStream is not that convenient if you are reading
a text file.
http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html
check this one.
【在 y****n 的大作中提到】 : 我知道FileInputStream, 但它的read()方法质只读进一个byte,我想得到所有数据 : 的byte[]
|
h*****0 发帖数: 4889 | 8 ...
【在 g*****g 的大作中提到】 : FileInputStream is not that convenient if you are reading : a text file. : http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html : check this one.
|
y****n 发帖数: 192 | 9 这个byte[]的length不知道啊
【在 h*****0 的大作中提到】 : ...
|
h*****0 发帖数: 4889 | 10 ...
有read(byte[] array, int offset, int length)...
【在 y****n 的大作中提到】 : 这个byte[]的length不知道啊
|
|
|
h*****0 发帖数: 4889 | 11 你写java不用sun的文档的?
【在 y****n 的大作中提到】 : 这个byte[]的length不知道啊
|
m******t 发帖数: 2416 | 12
This approach is wrong, actually. It
assumes that the file is in utf-8, which
is neither necessary, nor necessarily true.
【在 y****n 的大作中提到】 : 这个byte[]的length不知道啊
|
y****n 发帖数: 192 | 13 恩,我是指不知道读进来的file有多少bytes,怎么事先确定length啊??
【在 h*****0 的大作中提到】 : ... : 有read(byte[] array, int offset, int length)...
|
h*****0 发帖数: 4889 | 14 ...
file.size()
同学,你真的应该使用jdk文档。这些东西很基础的。看你写的东西却又止这么基础的
水平。
【在 y****n 的大作中提到】 : 恩,我是指不知道读进来的file有多少bytes,怎么事先确定length啊??
|
y****n 发帖数: 192 | 15 恩, 我犯糊涂了。
按照这个思路写了一下:
public static byte[] getByteDataFromFile(){
File f = new File(rawDataFile);
RandomAccessFile rf;
byte[] rawByteData = null;
try {
rf = new RandomAccessFile(rawDataFile, "r");
rawByteData = new byte[(int) rf.length()];
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream in = null;
【在 h*****0 的大作中提到】 : ... : file.size() : 同学,你真的应该使用jdk文档。这些东西很基础的。看你写的东西却又止这么基础的 : 水平。
|
h*****0 发帖数: 4889 | 16 1. 你现在的方法,无非是用RandomAccessFile来得到length,可是我之前说过了可以
直接用File#size()来得到长度。再者你得保证文件不太长才能直接用一个byte[]一存。
2. 你用Reader读出来的是字符串,再用UTF-8转化为字节串,比直接读出来的长,说明
你的文件并不是UTF-8编码,并且我可以猜测你的文件应该是中文。默认编码多半是GBK。
【在 y****n 的大作中提到】 : 恩, 我犯糊涂了。 : 按照这个思路写了一下: : public static byte[] getByteDataFromFile(){ : File f = new File(rawDataFile); : RandomAccessFile rf; : byte[] rawByteData = null; : try { : rf = new RandomAccessFile(rawDataFile, "r"); : rawByteData = new byte[(int) rf.length()]; : } catch (FileNotFoundException e2) {
|
y****n 发帖数: 192 | 17 谢谢!
存。
是说File有API可以直接得到size()? 我能找得的只有file.getTotalSpace()
GBK。
我的file全部是英文,另外我把UTF-8去掉也是同样结果。感觉不是这个原因。
【在 h*****0 的大作中提到】 : 1. 你现在的方法,无非是用RandomAccessFile来得到length,可是我之前说过了可以 : 直接用File#size()来得到长度。再者你得保证文件不太长才能直接用一个byte[]一存。 : 2. 你用Reader读出来的是字符串,再用UTF-8转化为字节串,比直接读出来的长,说明 : 你的文件并不是UTF-8编码,并且我可以猜测你的文件应该是中文。默认编码多半是GBK。
|
A**o 发帖数: 1550 | 18 read api pls...
http://java.sun.com/javase/6/docs/api/java/io/File.html#length()
【在 y****n 的大作中提到】 : 谢谢! : : 存。 : 是说File有API可以直接得到size()? 我能找得的只有file.getTotalSpace() : GBK。 : 我的file全部是英文,另外我把UTF-8去掉也是同样结果。感觉不是这个原因。
|
F****n 发帖数: 3271 | 19 What about if the file size is 1000MB?
存。
GBK。
【在 h*****0 的大作中提到】 : 1. 你现在的方法,无非是用RandomAccessFile来得到length,可是我之前说过了可以 : 直接用File#size()来得到长度。再者你得保证文件不太长才能直接用一个byte[]一存。 : 2. 你用Reader读出来的是字符串,再用UTF-8转化为字节串,比直接读出来的长,说明 : 你的文件并不是UTF-8编码,并且我可以猜测你的文件应该是中文。默认编码多半是GBK。
|
h*****0 发帖数: 4889 | 20 what do you mean? 1000MB is not big enough, you can still use a byte[] to
store it.
【在 F****n 的大作中提到】 : What about if the file size is 1000MB? : : 存。 : GBK。
|
F****n 发帖数: 3271 | 21 You should try it before you say so.
【在 h*****0 的大作中提到】 : what do you mean? 1000MB is not big enough, you can still use a byte[] to : store it.
|
A**o 发帖数: 1550 | 22 since op never thought about it, i assume he/she's dealing with small data
set.
otherwise, what's the point for getting the output as a complete byte[]?
but, i could be wrong. :)
【在 F****n 的大作中提到】 : You should try it before you say so.
|