字节输入输出流读写文件

可比克_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();
}
...全文
341 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);
            }
        }
    }
       
    
    
    

}
数据集可视化效果可参见下方展示。 【数据集概况】 · 检测类别(中文):[保龄球(bowling)] · 训练集:594 张 · 验证集:75 张 · 测试集:74 张 · 总计:743 张 该数据集聚焦于室内保龄球馆场景,系统性采集了多角度、多姿态下保龄球在不同运动阶段的视觉特征,为保龄球运动过程中的球体识别与轨迹分析提供了高质量标注样本,具有明确的体育训练与智能辅助系统开发价值。... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 100 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9938** mAP50-95 | 0.6966 Precision | 0.9740 Recall | 0.9974 train/box_loss | 0.9113 train/cls_loss | 0.2862 val/box_loss | 1.1516 val/cls_loss | 0.3116 【训练过程分析】 100 轮训练后 mAP50 达到 0.9938,模型收敛良好。Loss 曲线前段快速下降,后段趋于平稳,val_loss 无反弹,没有明显过拟合。但 mAP50-95 为 0.6966,和 mAP50 差距 0.30,定位精度仍有优化空间。 【模型性能评估】 Precision 0.9740、Recall 0.9974,精召双高,模型对保龄球的检测能力强。 【预测效果展示】 验证集预测效果较好,检测框基本准确覆盖保龄球,置信度整体偏高。 【改进建议】 1. 丰富场景多样性:补充不同光照、背景和遮挡条件下的样本。 2. 提升输入分辨率:640 ...
PaddleOCR 与YOLO目标检测 HTTP 服务。 项目通过 PaddleOCROnnx 原生库集成 OnnxRuntime加速能力,围绕 ONNX 模型部署, 以统一接口提供图像文字识别、YOLO 目标检测和 Tensor 数据输出。 功能概览 文字识别:支持图片 Base64、multipart/form-data 上传,以及文本和 JSON 结果。 目标检测:支持 YOLO 图片检测,返回检测框 JSON 或原始 Tensor。 浏览器演示:访问服务根地址,上传图片并切换 OCR、YOLO 模式。 健康检查:通过 /health 查看服务及 OCR、YOLO 引擎初始化状态。 并发处理:通过 OCR 引擎实例池处理并发请求,可调整实例数量。 体验与调用 启动后访问: 入口 地址 浏览器演示 http://localhost:5000/ 健康检查 http://localhost:5000/health 原生依赖 Windows:优先从 runtimes/win-x64/native/ 加载原生 DLL;该目录没有 PaddleOCROnnx.dll 时,尝试从可执行文件所在目录加载。主 DLL 与对应后端依赖应放在同一原生目录中,不要将同一组依赖分散到多个目录。 Linux:原生运行时位于 runtimes/linux-x64/native/,程序使用相对于可执行文件的运行时搜索路径查找随包部署的依赖。 后端选择:CoreOCROnnx 支持 ONNX Runtime、OpenVINO、TensorRT 后端。请使用与平台、进程架构、硬件和后端匹配的一整套运行时,不要混用不同后端或版本的依赖。

62,621

社区成员

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

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