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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

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
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class ScoreProcessor { public static void main(String[] args) { // 数据库连接信息 String jdbcURL = "jdbc:mysql://localhost:3306/Student"; String username = "root"; String password = "123456"; // 文件路径 String filePath = "考试成绩.txt"; Connection connection = null; Statement statement = null; BufferedReader br = null; try { // 1. 连接数据库 connection = DriverManager.getConnection(jdbcURL, username, password); // 2. 创建Score表(如果不存在) String createTableSQL = "CREATE TABLE IF NOT EXISTS Score (" + "学号 VARCHAR(10) PRIMARY KEY, " + "姓名 VARCHAR(50), " + "数学 INT, " + "语文 INT, " + "英语 INT)"; statement = connection.createStatement(); statement.executeUpdate(createTableSQL); // 3. 读取文件 br = new BufferedReader(new FileReader(filePath)); String line; boolean headerSkipped = false; while ((line = br.readLine()) != null) { if (!headerSkipped) { headerSkipped = true; // 跳过表头 continue; } // 4. 解析每一行数据 String[] data = line.trim().split("\\s+"); if (data.length != 5) { System.out.println("跳过无效行: " + line); continue; } String studentId = data[0]; String name = data[1]; int math = Integer.parseInt(data[2]); int chinese = Integer.parseInt(data[3]); int english = Integer.parseInt(data[4]); int totalScore = math + chinese + english; // 5. 如果总分超过240分,插入数据库 if (totalScore > 240) { String insertSQL = String.format( "INSERT INTO Score (学号, 姓名, 数学, 语文, 英语) VALUES ('%s', '%s', %d, %d, %d)", studentId, name, math, chinese, english); statement.executeUpdate(insertSQL); System.out.println("插入学生: " + name + ", 总分: " + totalScore); } } System.out.println("处理完成"); } catch (Exception e) { e.printStackTrace(); } finally { // 6. 关闭资源 try { if (br != null) br.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (IOException | SQLException e) { e.printStackTrace(); } } } }运行此代码
06-13
private void onMessageFsReserveSpace(Message msg) { 1799 String fsReserveSwitch = (null != msg.obj) ? (String) msg.obj : "On"; 1800 final StatFs statFs = new StatFs("/data"); 1801 String mReserveSpacePath = ""; 1802 String dataLabel = ""; 1803 String fsType = ""; 1804 BufferedReader br = null; 1805 BufferedWriter bw = null; 1806 long fsReservedBlocks = 0; 1807 long reserveBlocks = 0; 1808 long deltaBlocks = 0; 1809 double reservedStartRatio = 0.2; 1810 double reservedRatio = 0.025; 1811 String devPath = null; 1812 1813 //totalBlocks & validBlocks 1814 long totalBlocks = (long)statFs.getBlockCount(); 1815 long validBlocks = totalBlocks - (long)statFs.getAvailableBlocks(); 1816 1817 // ext4 or f2fs ? 1818 File mounts = new File("/proc/mounts"); 1819 try { 1820 br = new BufferedReader(new FileReader(mounts)); 1821 String s = null; 1822 while ((s = br.readLine()) != null) { 1823 if (s.indexOf(" /data f2fs", 0) != -1) { 1824 fsType = "f2fs"; 1825 devPath = s.split(" ")[0]; 1826 break; 1827 } else if (s.indexOf(" /data ext4", 0) != -1) { 1828 fsType = "ext4"; 1829 devPath = s.split(" ")[0]; 1830 break; 1831 } 1832 } 1833 } catch (Exception e) { 1834 Slog.e(TAG, "FsReserveSpace read fs type failed."); 1835 } finally { 1836 if (null != br) { 1837 try { 1838 br.close(); 1839 } catch (Exception e) { 1840 //silent. 1841 } 1842 } 1843 } 1844 Slog.i(TAG, "FsReserveSpace data partition fstype:" + fsType); 1845 Slog.i(TAG, "FsReserveSpace data device path:" + devPath); 1846 // data partition label 1847 try { 1848 Path link = Files.readSymbolicLink(Paths.get(devPath)); 1849 if (link != null) { 1850 devPath = link.toString(); 1851 } 1852 } catch (Exception e) { 1853 StringWriter sw = new StringWriter(); 1854 PrintWriter pw = new PrintWriter(sw); 1855 e.printStackTrace(pw); 1856 Slog.e(TAG, "FsReserveSpace read data label failed." + sw.toString()); 1857 } 1858 if (devPath != null) { 1859 dataLabel = devPath.split("/")[3]; 1860 Slog.i(TAG, "FsReserveSpace data label:" + dataLabel); 1861 } else { 1862 Slog.e(TAG, "FsReserveSpace read data label failed."); 1863 return; 1864 } 1865 //adjust reserve space 1866 if (fsType.equals("f2fs")) { 1867 mReserveSpacePath = "/sys/fs/f2fs/" + dataLabel + "/reserved_blocks"; 1868 } else if (fsType.equals("ext4")) { 1869 mReserveSpacePath = "/sys/fs/ext4/" + dataLabel + "/reserved_clusters"; 1870 } else { 1871 Slog.e(TAG, "FsReserveSpace unsupported fs type: " + fsType); 1872 return; 1873 } 1874 1875 //restoring fsReservedBlocks 1876 fsReservedBlocks = SystemProperties.getInt("persist.sys.oppo.fsReservedBlocks", -1); 1877 if (fsReservedBlocks == -1) { 1878 try { 1879 br = new BufferedReader(new FileReader(mReserveSpacePath)); 1880 fsReservedBlocks = Integer.valueOf(br.readLine()); 1881 br.close(); 1882 br = null; 1883 } catch (Exception e) { 1884 Slog.e(TAG, "FsReserveSpace read failed :" + mReserveSpacePath); 1885 } finally { 1886 if (null != br) { 1887 try { 1888 br.close(); 1889 } catch (Exception e) { 1890 //silent. 1891 } 1892 } 1893 } 1894 } 1895 1896 // reserve after 20% space used, ~2% space reserved when 100% space used, default reserved: 0 or 4096(for EXT4) 1897 // reserve blocks = 0.025 * totalBlocks * (max(0.2, validBlocks / totalBlocks) - 0.2) 1898 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); 1899 if (fsReservedBlocks == reserveBlocks) { 1900 if (!mFirstReserve) { 1901 return; 1902 } 1903 mFirstReserve = false; 1904 } else if (fsReservedBlocks < reserveBlocks) { 1905 deltaBlocks = reserveBlocks - fsReservedBlocks; 1906 //no enough free blocks (<= 400 MB after reserve) to reserve 1907 if (deltaBlocks >= (totalBlocks - validBlocks - UNUSABLE_RESERVE_BLOCKS)) { 1908 return; 1909 } 1910 deltaBlocks = (deltaBlocks < DELTA_RESERVE_BLOCKS) ? deltaBlocks : DELTA_RESERVE_BLOCKS; 1911 } else { 1912 deltaBlocks = fsReservedBlocks - reserveBlocks; 1913 deltaBlocks = (deltaBlocks < DELTA_RESERVE_BLOCKS) ? -deltaBlocks : -DELTA_RESERVE_BLOCKS; 1914 } 1915 fsReservedBlocks += deltaBlocks; 1916 1917 //saving fsReservedBlocks 1918 SystemProperties.set("persist.sys.oppo.fsReservedBlocks", Long.toString(fsReservedBlocks)); 1919 1920 try { 1921 bw = new BufferedWriter(new FileWriter(mReserveSpacePath)); 1922 bw.write(Long.toString(fsReservedBlocks)); 1923 bw.close(); 1924 bw = null; 1925 } catch (Exception exce) { 1926 Slog.e(TAG, "FsReserveSpace write failed for:" + mReserveSpacePath); 1927 } finally { 1928 if (null != bw) { 1929 try { 1930 bw.close(); 1931 } catch (Exception e) { 1932 //silent. 1933 } 1934 } 1935 } 1936 } 1937 1938 //#ifdef OPLUS_FEATURE_STORAGE 1939 //shubin@.STORAGE, 2021/10/27, add for SD PASSWORD 1940 private volatile IVold mVold; 1941
07-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值