日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java按列读取数据再存储_Java指定行读写数据

發(fā)布時(shí)間:2023/12/3 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java按列读取数据再存储_Java指定行读写数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

/**

* 根據(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。