java 写入文件一行数据
java读取文件一行数据
只是案例代码
/** * 判断该坐标是否已经转换 */ public static String isRead(String str) { try { File file = new File("parseLocation.txt"); if(!file.exists()){ file.createNewFile(); } BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件 String line =""; while ((line = br.readLine()) != null) { if((line.substring(0,line.indexOf(":"))).equals(str)){ return line.substring(line.indexOf(":")+1); } } br.close(); } catch (Exception e) { // TODO: handle exception } return "1"; } /** * 写入文件 */ public static void write(String key,String val) { try { if(!"1".equals(isRead(key))){ return; }; File file = new File("parseLocation.txt"); if(!file.exists()){ file.createNewFile(); } FileOutputStream stream = new FileOutputStream(file, true); stream.write((key+":"+val+System.getProperty("line.separator")).getBytes()); stream.flush(); stream.close(); } catch (Exception e) { // TODO: handle exception } }