s = br.readLine();//这行怎么读取不到内容???

本文探讨了使用Java进行文件读取时遇到的问题,特别是针对BufferedReader读取内容为空的情况进行了详细分析。通过示例代码展示了如何初始化文件路径并逐行读取文件内容。

 File f = new File(path);
  FileReader fr = new FileReader(f);
  BufferedReader br = new BufferedReader(fr);
  String s;
  try
  {
   s = br.readLine();
   while (s != null)
   {
    s += s;
    text.setText(new String(s));
    System.out.println(s);    
    s = br.readLine();//这行怎么读取不到内容???
   }
   //text.setText(new String(s));
  } catch (IOException e)
  {
   System.out.println("打开出错");
  } finally

package Graph; import java.util.Stack; public class DepthFirstPaths { private boolean[] marked; private int[] edgeTo; private int s; public DepthFirstPaths(Graph G, int s) { this.marked=new boolean[G.V()]; this.edgeTo=new int[G.V()]; this.s=s; dfs(G,s); } private void dfs(Graph G, int v){ marked[v]=true; for(int w:G.adj(v)){ if(!marked[w]){ edgeTo[w]=v; } dfs(G,w); } } public boolean hasPathTo(int v){ return marked[v]; } public Stack<Integer> pathTo(int v){ Stack<Integer> path=new Stack<>(); while(!hasPathTo(v)){ path.push(edgeTo[v]); } path.push(v); return path; } } package Graph; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Stack; public class DepthFirstPathsTest { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(DepthFirstPaths.class.getClassLoader().getResourceAsStream("road_find.txt"))); String total = br.readLine(); Graph g=new Graph(Integer.parseInt(total)); String edgeNumbers = br.readLine(); for(int i=1;i<=edgeNumbers.length();i++){ String edge = br.readLine(); String[] s = edge.split(" "); int i1 = Integer.parseInt(s[0]); int i2 = Integer.parseInt(s[1]); g.addEdge(i1,i2); } DepthFirstPaths path=new DepthFirstPaths(g,0); Stack<Integer> paths = path.pathTo(4); StringBuilder sbs=new StringBuilder(); for (Integer i : paths) { sbs.append(i+"-->"); } sbs.deleteCharAt(sbs.length()-1); System.out.println(sbs); } } 以下是代码中提到的txt文件: 6 8 0 2 0 1 2 1 2 3 2 4 3 5 3 4 0 5 为什么报错
11-01
package com.example.demo.controller; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.demo.util.MediaUtils; import lombok.extern.slf4j.Slf4j; /** * @Description: 视频输出 @Author: cq @Date: 2022/11/30 @Version: V1.0 */ @RestController @RequestMapping("/video/videos") @Slf4j public class videoTestController { private static final String ffmpegPath = "D:/ffmpeg-6.0-essentials_build/bin/ffmpeg.exe"; /** 视频播放指令执行 rtsp地址【源播放地址】通过ffmpeg程序进行转化成rtmp,将地址传给flv.js进行播放 */ @GetMapping("/videoStart") public String videoPreview(@RequestParam(name = "videoPath", required = false) String videoPath) { System.out.println(videoPath); int lastSlashIndex=videoPath.lastIndexOf('/'); String channelNo=videoPath.substring(lastSlashIndex+1,lastSlashIndex+2); String cmd ="cmd /k start "+ffmpegPath +" -fflags nobuffer -flags low_delay -analyzeduration 100000 -probesize 32" +" -rtsp_transport tcp -i" + " " + videoPath + " " + "-s 1280x720 -r 30 -c:v libx264 -preset veryfast -tune zerolatency -g 30 -keyint_min 30 -sc_threshold 0" + " -c:a aac -f flv -an rtmp://109.136.146.133:1935/live/channel"+channelNo; try { if(!MediaUtils.startStream(videoPath, channelNo, cmd)) { return "http://109.136.146.133:9000/live?port=1935&app=live&stream=channel"+channelNo; } } catch (IOException e) { e.printStackTrace(); } return "http://109.136.146.133:9000/live?port=1935&app=live&stream=channel"+channelNo; } /** 关闭ffmpeg.exe程序 */ @GetMapping("/videoClose") public String close() { closeHistoryProgram("ffmpeg.exe"); return "已成功停止"; } public void closeHistoryProgram(String processName) { String cmd = "taskkill /f /t /im " + processName; try { // exec执行cmd命令 Process process = Runtime.getRuntime().exec(cmd); // 获取CMD命令结果的输出流 InputStream fis = process.getInputStream(); InputStreamReader isr = new InputStreamReader(fis, "GBK"); // 使用缓冲器读取 BufferedReader br = new BufferedReader(isr); String line = null; // 全部读取完成为止,一行一行读取 while ((line = br.readLine()) != null) { // 输出读取内容 System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }现在打开多个视频流的时候,我在前端关闭某一个视频流,后端会关闭所有ffmpeg进程,导致所有打开的视频都不能播放,这应该怎么解决
10-15
private void onMessageFsReserveSpace(Message msg) { String fsReserveSwitch = (null != msg.obj) ? (String) msg.obj : "On"; final StatFs statFs = new StatFs("/data"); String mReserveSpacePath = ""; String dataLabel = ""; String fsType = ""; BufferedReader br = null; BufferedWriter bw = null; long fsReservedBlocks = 0; long reserveBlocks = 0; long deltaBlocks = 0; double reservedStartRatio = 0.2; double reservedRatio = 0.025; String devPath = null; //totalBlocks & validBlocks long totalBlocks = (long)statFs.getBlockCount(); long validBlocks = totalBlocks - (long)statFs.getAvailableBlocks(); // ext4 or f2fs ? File mounts = new File("/proc/mounts"); try { br = new BufferedReader(new FileReader(mounts)); String s = null; while ((s = br.readLine()) != null) { if (s.indexOf(" /data f2fs", 0) != -1) { fsType = "f2fs"; devPath = s.split(" ")[0]; break; } else if (s.indexOf(" /data ext4", 0) != -1) { fsType = "ext4"; devPath = s.split(" ")[0]; break; } } } catch (Exception e) { Slog.e(TAG, "FsReserveSpace read fs type failed."); } finally { if (null != br) { try { br.close(); } catch (Exception e) { //silent. } } } Slog.i(TAG, "FsReserveSpace data partition fstype:" + fsType); Slog.i(TAG, "FsReserveSpace data device path:" + devPath); // data partition label try { Path link = Files.readSymbolicLink(Paths.get(devPath)); if (link != null) { devPath = link.toString(); } } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Slog.e(TAG, "FsReserveSpace read data label failed." + sw.toString()); } if (devPath != null) { dataLabel = devPath.split("/")[3]; Slog.i(TAG, "FsReserveSpace data label:" + dataLabel); } else { Slog.e(TAG, "FsReserveSpace read data label failed."); return; } //adjust reserve space if (fsType.equals("f2fs")) { mReserveSpacePath = "/sys/fs/f2fs/" + dataLabel + "/reserved_blocks"; } else if (fsType.equals("ext4")) { mReserveSpacePath = "/sys/fs/ext4/" + dataLabel + "/reserved_clusters"; } else { Slog.e(TAG, "FsReserveSpace unsupported fs type: " + fsType); return; } //restoring fsReservedBlocks fsReservedBlocks = SystemProperties.getInt("persist.sys.oppo.fsReservedBlocks", -1); if (fsReservedBlocks == -1) { try { br = new BufferedReader(new FileReader(mReserveSpacePath)); fsReservedBlocks = Integer.valueOf(br.readLine()); br.close(); br = null; } catch (Exception e) { Slog.e(TAG, "FsReserveSpace read failed :" + mReserveSpacePath); } finally { if (null != br) { try { br.close(); } catch (Exception e) { //silent. } } } } // reserve after 20% space used, ~2% space reserved when 100% space used, default reserved: 0 or 4096(for EXT4) // reserve blocks = 0.025 * totalBlocks * (max(0.2, validBlocks / totalBlocks) - 0.2) reserveBlocks = ((validBlocks > (reservedStartRatio * totalBlocks)) && fsReserveSwitch.equals("On")) ? (long)(reservedRatio * (validBlocks - reservedStartRatio * totalBlocks)) : (fsType.equals("ext4") ? INIT_RESERVED_EXT4_BLOCK_SIZE : INIT_RESERVED_F2FS_BLOCK_SIZE); if (fsReservedBlocks == reserveBlocks) { if (!mFirstReserve) { return; } mFirstReserve = false; } else if (fsReservedBlocks < reserveBlocks) { deltaBlocks = reserveBlocks - fsReservedBlocks; //no enough free blocks (<= 400 MB after reserve) to reserve if (deltaBlocks >= (totalBlocks - validBlocks - UNUSABLE_RESERVE_BLOCKS)) { return; } deltaBlocks = (deltaBlocks < DELTA_RESERVE_BLOCKS) ? deltaBlocks : DELTA_RESERVE_BLOCKS; } else { deltaBlocks = fsReservedBlocks - reserveBlocks; deltaBlocks = (deltaBlocks < DELTA_RESERVE_BLOCKS) ? -deltaBlocks : -DELTA_RESERVE_BLOCKS; } fsReservedBlocks += deltaBlocks; //saving fsReservedBlocks SystemProperties.set("persist.sys.oppo.fsReservedBlocks", Long.toString(fsReservedBlocks)); try { bw = new BufferedWriter(new FileWriter(mReserveSpacePath)); bw.write(Long.toString(fsReservedBlocks)); bw.close(); bw = null; } catch (Exception exce) { Slog.e(TAG, "FsReserveSpace write failed for:" + mReserveSpacePath); } finally { if (null != bw) { try { bw.close(); } catch (Exception e) { //silent. } } } }结合之前log和这段代码分析devPath是从哪里来的,为什么报错
07-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值