我们都知道文件是由一个字节一个字节组成的?怎样才能一个字节一个字节的返回它们呢,因为我想对每个字节处理后,再把它们合并起来!比如文件是由下面组成:note:每个字母表示一个1024字节的内容
----------------
A|B|C|D|E|F|G|H|
----------------伪代码:
while(文件没结束){
value= ...... //怎样做?
}
System.out.println(value);
结果应该是:A
B
C
.......
----------------
A|B|C|D|E|F|G|H|
----------------伪代码:
while(文件没结束){
value= ...... //怎样做?
}
System.out.println(value);
结果应该是:A
B
C
.......
从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。public int read(byte[] b) throws IOException
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。如果要一个字节一个字节读,就使用read方法,如果要一下子读入若干个字节,就使用read(byte[] b)方法,比如你可以一下子读入1024个.
import java.io.FileInputStream;
import java.io.InputStreamReader;public class Test {
public static String readFile(String path) throws Exception {
StringBuffer sb = new StringBuffer();
FileInputStream fin = new FileInputStream(path);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(path)));
char temp[] = new char[1];
for (int i = 0; (i = in.read(temp)) != -1;) {
sb.append(temp, 0, i);
System.out.println(temp[0]);
}
fin.close();
return new String(sb);
} public static void main(String[] args) throws Exception {
readFile("c:/cmd.txt");
}
}
http://blog.csdn.net/sunyujia/archive/2008/06/15/2551297.aspx
public class Test {
public static String readFile(String path) throws Exception {
FileInputStream in = new FileInputStream(path);
byte[] buffer = new byte[1024];// 缓冲区
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
in.close();
return new String(bos.toByteArray());
} public static void main(String[] args) throws Exception {
System.out.println(readFile("c:/cmd.txt"));
}
}
public class Test {
public static String readFile(String path) throws Exception {
FileInputStream in = new FileInputStream(path);
byte[] buffer = new byte[1];// 缓冲区
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {
bos.write(buffer, 0, bytesRead);
System.out.println(new String(buffer, 0, bytesRead));
Arrays.fill(buffer, (byte) 0);
}
in.close();
return new String(bos.toByteArray());
} public static void main(String[] args) throws Exception {
readFile("c:/cmd.txt");
}
}
String fn = "e:/test.txt";
InputStream in = new FileInputStream(fn);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b;
while( (b = in.read())!= -1 )
{
bos.write(b); //放入缓冲区
//b中保存的就是当前读取到的字节,可以在此对它进行操作
char c = (char)b;
System.out.println( c );
}
in.close();
//合并之后的内容
System.out.println( new String(bos.toByteArray()) );
}
String fileName = "D:\\abc.txt";
final int buffSize = 1024;//待处理字符块的大小,后面用于创建缓冲区。
final int seprSize = 1;//分割符所占字节数
InputStream in = new FileInputStream(fileName);
byte [] buff = new byte[buffSize];//创建buffSize大小的缓冲区,存放字节数据。
while(buffSize == in.read(buff)){//将文件内容读入缓冲区,当读入数据的多少等于buffSize时,进入循环体。
proccess(buff);//处理缓冲区的数据。
for(int i=0;i<seprSize;i++){//用于读取分割符的循环结构。
if(in.read()== -1)return;
}
buff = new byte[buffSize];
}
}
private static void proccess(byte [] buff){
//A|B|C|D|E|F|G|H| 中字母的内容就在buff中,每个buff大小为1024;
System.out.println(new String(buff));//按字符串的形式输出(默认的字符编码)
}
100分就能勾引一堆高手呀
哪天没心思做了,我也拿100分去勾引高手去&(#¥&(@#*……¥
FileInputStream fin = new FileInputStream("test");
byte b = (byte)fin.read();
Vector<Byte> content = new Vector<Byte>();
while (b != -1)
{
content.add(b);
b = (byte)fin.read();
}
遍历这个Vector就可以实现对每个字节进行操作了, 一定要注意字节里包含负数, 它代表的不一定是负数.....
至于如何再编码,就得看具体的需求了