java按列读取数据再存储_Java指定行读写数据
/**
* 根據(jù)指定行寫數(shù)據(jù)
*
* @param lineNumber 要存的行數(shù)
* @param data 要存儲(chǔ)的數(shù)據(jù)
*/
public static void setAppointedLineNumber(int lineNumber, String data) throws IOException {
Path path = Paths.get(configuration);
List lines = Files.readAllLines(path, StandardCharsets.UTF_8);
lines.set(lineNumber - 1, data);
Files.write(path, lines, StandardCharsets.UTF_8);
}
/**
* 1.根據(jù)指定行讀數(shù)據(jù)
*
* @param lineNumber
*/
public static String readAppointedLineNumber(int lineNumber) {
String appointedLine = "";
FileReader in = null;
LineNumberReader reader = null;
try {
in = new FileReader(configuration);
reader = new LineNumberReader(in);
long totalLine = Files.lines(Paths.get(configuration)).count();
if (lineNumber < 0 || lineNumber > totalLine) {
throw new Exception("指定行【" + lineNumber + "】不在文件行數(shù)范圍內(nèi)");
}
int line = 1;
reader.setLineNumber(lineNumber);
long i = reader.getLineNumber();
String s = "";
while ((s = reader.readLine()) != null) {
if (i == line) {
appointedLine = s;
break;
}
line++;
}
return appointedLine;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeResource(in, reader);
}
return appointedLine;
}
/**
* 2.關(guān)閉資源
*
* @param in
* @param reader
*/
public static void closeResource(FileReader in, LineNumberReader reader) {
try {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
標(biāo)簽:Java,String,讀寫,reader,指定,param,printStackTrace,lineNumber,null
來(lái)源: https://blog.csdn.net/sz793919425/article/details/110920319
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java按列读取数据再存储_Java指定行读写数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 域名证书怎么获得(域名证书怎么获得的)
- 下一篇: pdfbox java.lang.out