ioint read() Reads the next byte of data from the input stream. int read(byte[] b) Reads some number of bytes from the input stream and stores them into the buffer array b. int read(byte[] b, int off, int len) Reads up to len bytes of data from the input stream into an array of bytes.
1.XKP(低等下人) 的做法是错的。 2.takecare(大厅) 的说法显然不完全。看了后也不知道哪个类,没意义。 3.whyxx(风之子) 的做法很好。例子也对。 但在某些情况下也不对。 例如,如果文件内,含有汉字,文件上是按UTF-8存储的,读到内存转换后会变成双字节。UTF-8的字节数是变长的。显然就不对了。 4.正确的做法如下: public class TestByte { public TestByte() { } public static void main(String[] args) throws Exception { java.io.RandomAccessFile ras = new java.io.RandomAccessFile("e:/tmp/01_2003_05_03.txt","r"); int total = (int)ras.length(); System.out.println(total); byte[] file = new byte[total]; int p=0; while (total-p>0) { p+=ras.read(file,p,total-p); } ras.close(); } }
根据楼上兄弟的帮助,我的功能实现了,并且编写了一个更简单的方式:如下: public static void main(String[] args) throws Exception { java.io.FileInputStream ras = new java.io.FileInputStream("E:/hehao/JavaProgramm/XMLProject/src/xmlproject/gameTest.xml"); int total = ras.available() ; byte[] file = new byte[total]; ras.read(file); ras.close(); }
还有一个问题,如何将字节数组中的信息使用System.out.println方法将信息打印出来?
System.out.println( new String( byteArray ) );
你的代码有严重的问题。 不能这么简单的读区。public static void main(String[] args) throws Exception { java.io.FileInputStream ras = new java.io.FileInputStream("E:/hehao/JavaProgramm/XMLProject/src/xmlproject/gameTest.xml"); int total = ras.available() ; byte[] file = new byte[total]; ras.read(file); ras.close(); }
Reads the next byte of data from the input stream.
int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes.
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String str = reader.readLine();
while(str != null) {
buf += str;
buf += "\n";
str = reader.readLine();
}
reader.close();
byte[] temp = buf.getBytes();
2.takecare(大厅) 的说法显然不完全。看了后也不知道哪个类,没意义。
3.whyxx(风之子) 的做法很好。例子也对。
但在某些情况下也不对。
例如,如果文件内,含有汉字,文件上是按UTF-8存储的,读到内存转换后会变成双字节。UTF-8的字节数是变长的。显然就不对了。
4.正确的做法如下:
public class TestByte
{
public TestByte()
{
}
public static void main(String[] args) throws Exception
{
java.io.RandomAccessFile ras = new java.io.RandomAccessFile("e:/tmp/01_2003_05_03.txt","r");
int total = (int)ras.length();
System.out.println(total);
byte[] file = new byte[total];
int p=0;
while (total-p>0)
{
p+=ras.read(file,p,total-p);
}
ras.close();
}
}
public static void main(String[] args) throws Exception
{
java.io.FileInputStream ras = new java.io.FileInputStream("E:/hehao/JavaProgramm/XMLProject/src/xmlproject/gameTest.xml");
int total = ras.available() ;
byte[] file = new byte[total];
ras.read(file);
ras.close();
}
不能这么简单的读区。public static void main(String[] args) throws Exception
{
java.io.FileInputStream ras = new java.io.FileInputStream("E:/hehao/JavaProgramm/XMLProject/src/xmlproject/gameTest.xml");
int total = ras.available() ;
byte[] file = new byte[total];
ras.read(file);
ras.close();
}