覆盖java txt文件中的一行
所以我创建了这个方法,它在最后显示整行,因为我在转换和编辑数组后显示数组。所以我的问题是,我如何知道将数组覆盖到我从中获取它的同一行。提前谢谢,这是我的代码覆盖java txt文件中的一行,java,arrays,file,io,Java,Arrays,File,Io,所以我创建了这个方法,它在最后显示整行,因为我在转换和编辑数组后显示数组。所以我的问题是,我如何知道将数组覆盖到我从中获取它的同一行。提前谢谢,这是我的代码 public void getData(String path, String accountNumber) { try { FileInputStream fis = new FileInputStream(path); BufferedReader br = new Buffered
public void getData(String path, String accountNumber) {
try {
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
System.out.println("Please Enter the Deposit amount That you would like to add.");
Scanner sn = new Scanner (System.in);
int add = sn.nextInt();
String str;
while ((str = br.readLine()) != null) {
if (str.contains(accountNumber)) {
String[] array = str.split(" ");
int old = Integer.parseInt(array[3]);
int Sum= old + add;
String Sumf = Integer.toString(Sum);
array[3] = Sumf;
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);}
break;
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public void getData(字符串路径、字符串帐号){
试一试{
FileInputStream fis=新的FileInputStream(路径);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fis));
System.out.println(“请输入您要添加的存款金额。”);
扫描仪序列号=新扫描仪(System.in);
int add=sn.nextInt();
字符串str;
而((str=br.readLine())!=null){
if(str.contains(accountNumber)){
字符串[]数组=str.split(“”);
int old=Integer.parseInt(数组[3]);
整数和=旧+加;
字符串Sumf=Integer.toString(总和);
数组[3]=Sumf;
for(int i=0;i
我使用字符串accountNumber获取我需要的特定行。获取行后,我将其更改为数组,同时使用str.split(“”)拆分索引。之后,我知道我需要编辑索引编号[3]。所以我这样做,然后我把它放回数组中。我需要做的最后一件事是现在将阵列恢复到正确的位置 我就是这样理解这个问题的:你有一个文件,你会一行一行地读它,然后做一些更改,然后想在同一位置再次写入它。创建一个新的临时文件并将内容写入临时文件,如果更改,则写入更改的结果,如果不按原样写入。
最后,将临时文件重命名为原始文件名您可以跟踪正在读取的文件的输入,并使用修改后的版本将其写回
public void getData(String path, String accountNumber) {
try {
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
System.out.println("Please Enter the Deposit amount That you would like to add.");
Scanner sn = new Scanner (System.in);
int add = sn.nextInt();
String line; // current line
String input = ""; // overall input
while ((line = br.readLine()) != null) {
if (line.contains(accountNumber)) {
String[] array = line.split(" ");
int old = Integer.parseInt(array[3]);
int Sum= old + add;
String Sumf = Integer.toString(Sum);
array[3] = Sumf;
// rebuild the 'line' string with the modified value
line = "";
for (int i = 0; i < array.length; i++)
line+=array[i]+" ";
line = line.substring(0,line.length()-1); // remove the final space
}
// add the 'line' string to the overall input
input+=line+"\n";
}
// write the 'input' String with the replaced line OVER the same file
FileOutputStream fileOut = new FileOutputStream(path);
fileOut.write(input.getBytes());
fileOut.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
public void getData(字符串路径、字符串帐号){
试一试{
FileInputStream fis=新的FileInputStream(路径);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fis));
System.out.println(“请输入您要添加的存款金额。”);
扫描仪序列号=新扫描仪(System.in);
int add=sn.nextInt();
字符串行;//当前行
字符串输入=”;//总输入
而((line=br.readLine())!=null){
if(行包含(帐号)){
String[]数组=line.split(“”);
int old=Integer.parseInt(数组[3]);
整数和=旧+加;
字符串Sumf=Integer.toString(总和);
数组[3]=Sumf;
//使用修改后的值重新生成“line”字符串
第“”行;
for(int i=0;i
编辑完数组后,您希望再次将数组转换为字符串??因此,编辑完数组后,我现在就显示它。然后我想把它写在我抓到的那一行。让我们假设第1行包含:a b c 3,然后我抓起它并创建了数组,我的数组现在看起来像:数组[a,b,c,3],然后当我编辑它时,我使它看起来像数组[a,b,c,4],现在我希望第1行是:a b c 4,所以我将新数组写入到它的同一行,但我每次都会创建一个新文件。我可以在我抓取它的同一个txt文件中编辑那一行吗?如果你不同时编辑这两行,会更容易。运行整个文件,进行更改并在Stringbuilder中保持读取状态。然后,如果需要,将新文件移到旧文件上。您永远不希望同时读取和写入同一文件。每次写入时,文件中的偏移量都会移动,而读取光标将无法跟踪该偏移量。这是一家银行,因此每次需要写入时,我都需要输入一个不同的帐号,然后关闭文件。您能解释一下我输入的格式是什么意思吗?您还可以解释一下这行“input+=str+”\n“;”以及为什么我们有\n。我知道这是一个新行,但为什么?我只是说我不知道你正在阅读的文本文件是什么样子的。新行是因为在读取文件时,新行字符序列不会显示在字符串中,但在将字符串写回文件时是必需的。因此,我的文件是%s%s%d,我正在最后编辑数字。同时输入+=str+“\n”;具体来说,输入给我一个错误,比如import jdk.nashorn.tools.ShellFunctions.input,当我输入时,仍然会给我一个错误,请尝试复制代码的最新版本,因为我做了一些小的更改。它正在寻找jdk.nashorn.tools.ShellFunctions.input,因为它认为“input”是一个类而不是变量。我真的忘记声明/初始化了,哈哈,非常感谢。介意评论一下吗?这样我可以学到更多?至少你加了一点。非常感谢。