字节输入输出流读写文件

可比克_zhjq 2013-04-13 07:12:51
这个程序为什么只输出第一行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();
}
...全文
342 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
katrinaqian 2013-04-15
  • 打赏
  • 举报
回复
这样也可以:

while ((flag=in.read(temp)) != -1) {
    System.out.println(new String(temp));
}
摆烂办不到 2013-04-14
  • 打赏
  • 举报
回复
 int tempByte;
    while((tempByte=in.read())!=-1)
    {
System.out.write(tempByte);
    }
把这段改成
String s;
while((s = in.readLine()) != null) {
  System.out.println(s);
}
试试 readLine();一行一行的读
Candylibin 2013-04-14
  • 打赏
  • 举报
回复
Lz要操作文本文件 ,
BufferedReader bufr = 
				new BufferedReader(new InputStreamReader(file));
调readLine();
LoveAndroid520 2013-04-13
  • 打赏
  • 举报
回复
import java.io.File;
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();
				}
			}
		}
	}

}
LoveAndroid520 2013-04-13
  • 打赏
  • 举报
回复
import java.io.File;
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();
		}
	}

}
STEEL-CHINA 2013-04-13
  • 打赏
  • 举报
回复
其实你的已经输出来了。 out.write("12345789\r\n一二三四五六七八九".getBytes()); in.read()读一个byte,虽然是返回int,但实际上是从内存中读一个byte数据。 显示出来的时候,不是完整汉字。 你改为以下,看一下内存中的值就明白什么意思了。 输出为这个值吧: System.out.println("\t" + tempByte);
十年彩虹 2013-04-13
  • 打赏
  • 举报
回复
package IO;

import java.io.File;
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);
            }
        }
    }
       
    
    
    

}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧