这个程序为什么只输出第一行123456789
File file=new File("d:/1.txt");
try
{
file.createNewFile();
OutputStream out=new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
InputStream in=new FileInputStream(file);
int tempByte;
while((tempByte=in.read())!=-1)
{
System.out.write(tempByte);
}
}
catch (IOException e)
{
e.printStackTrace();
}io流
File file=new File("d:/1.txt");
try
{
file.createNewFile();
OutputStream out=new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
InputStream in=new FileInputStream(file);
int tempByte;
while((tempByte=in.read())!=-1)
{
System.out.write(tempByte);
}
}
catch (IOException e)
{
e.printStackTrace();
}io流
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;public class IO {
public static void main(String []args) {
//String fileName = "D:" + File.separator;
String fileName = "D:" + File.separator + "HelloWorld.txt";
File file = new File(fileName);
try {
//file.createNewFile();
//System.out.println("创建文件成功!");
//OutputStream out = new FileOutputStream(file,true);
//String str = "霹雳神兵";
// String str = "\r\n Rollen";
// byte [] b = str.getBytes();
// System.out.println(b);
// for(int i = 0; i < b.length; i++ ) {
// out.write(b[i]);
// }
// out.close();
// System.out.println("写入成功。");
InputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int len = in.read(b);
in.close();
System.out.println("长度" + len);
System.out.println("读取成功!" + new String(b,0,len));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
//print(file);
}
public static void print(File file) {
if(file != null) {
if(file.isDirectory()) {
File [] f = file.listFiles();
if(f != null) {
for(int i = 0; i< f.length; i++) {
print(f[i]);
}
}
} else {
System.out.println(file);
}
}
}
}
out.write("12345789\r\n一二三四五六七八九".getBytes());
in.read()读一个byte,虽然是返回int,但实际上是从内存中读一个byte数据。
显示出来的时候,不是完整汉字。
你改为以下,看一下内存中的值就明白什么意思了。输出为这个值吧:
System.out.println("\t" + tempByte);
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class FileTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("d:/1.txt");
try {
file.createNewFile();
OutputStream out = new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
InputStream in = new FileInputStream(file);
int tempByte;
byte[] b = new byte[1024];
while ((tempByte = in.read(b)) != -1) {
// System.out.write(tempByte);
System.out.write(b, 0, tempByte);
} } catch (IOException e) {
e.printStackTrace();
}
}}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class FileTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("d:/1.txt");
OutputStream out = null;
InputStream in = null;
try {
file.createNewFile();
out = new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
in = new FileInputStream(file);
int tempByte;
byte[] b = new byte[1024];
while ((tempByte = in.read(b)) != -1) {
// System.out.write(tempByte);
System.out.write(b, 0, tempByte);
} } catch (IOException e) {
e.printStackTrace();
} finally {//关闭流
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}}
while((tempByte=in.read())!=-1)
{
System.out.write(tempByte);
}
把这段改成String s;
while((s = in.readLine()) != null) {
System.out.println(s);
}试试 readLine();一行一行的读
BufferedReader bufr =
new BufferedReader(new InputStreamReader(file));调readLine();
System.out.println(new String(temp));
}