public class Main {
public static void main(String[] args) {
try {
InputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\a.txt");
byte[] bytes = new byte[2]; //这里我写2是为了演示,一般写1024吧
int n;
while ((n = in.read(bytes)) != -1) {
String s = new String(bytes,0,n);//这个不能直接写bytes,不然可能会读错
System.out.println(s);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
public class Main {
public static void main(String[] args) {
try {
InputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\a.txt");
OutputStream os = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\b.txt");
byte[] bytes = new byte[2];
int n;
while ((n = in.read(bytes)) != -1) {
os.write(bytes,0,n); //这里同样的不写n也会出现bug
}
in.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}