[英]How would I edit a specific line in a text file with java?
I am writing a program that lets the user enter up to 9999 accounts into a text file, however the issue i'm having is that they can be put in any order, but I have to print them in a sequential order.我正在编写一个程序,允许用户将最多 9999 个帐户输入到一个文本文件中,但是我遇到的问题是它们可以按任何顺序放置,但我必须按顺序打印它们。 Here's my code这是我的代码
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.text.*;
public class CreateBankFile {
public static int lines = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Path file = Paths.get("/root/sandbox/BankAccounts.txt");
String line = "";
int acctNum = 0;
String lastName;
double bal;
final int QUIT = 9999;
try
{
OutputStream output = new BufferedOutputStream(Files.newOutputStream(file));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
while(acctNum != QUIT)
{
System.out.print("Enter the acct num less than 9999: ");
acctNum = input.nextInt();
if(acctNum == QUIT)
{
continue;
}
System.out.print("Enter a last name: ");
lastName = input.next();
if(lastName.length() != 8)
{
if(lastName.length() > 8)
{
lastName = lastName.substring(0, 8);
}
else if(lastName.length() < 8)
{
int diff = 8 - lastName.length();
for(int i = 0; i < diff; i++)
{
lastName += " ";
}
}
}
System.out.print("Enter balance: ");
bal = input.nextDouble();
line = "ID#" + acctNum + " " + lastName + "$" + bal;
writer.write(line);
writer.newLine();
lines++;
}
writer.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
}
My question being, how can I get it so that when the user inputs "55" for example, it is printed to the 55th line of the text file?我的问题是,我怎样才能得到它,以便当用户输入“55”时,它被打印到文本文件的第 55 行?
You can do something like this:你可以这样做:
1) create a class that will store your line ( acctNum, lastName.. etc ) 1)创建一个 class 来存储你的行(acctNum,lastName ..等)
2) in your method, create an arraylist of the class you created, for a given number "n", your method will parse all the lines, if acctNum is less than "n", you will create a new instance using this line and add it to your arraylist 2)在你的方法中,创建一个你创建的class的arraylist,对于给定的数字“n”,你的方法将解析所有行,如果acctNum小于“n”,你将使用此行创建一个新实例并且将其添加到您的 arraylist
3) you will sort the arraylist using the acctNum and then print its content 3)您将使用 acctNum 对 arraylist 进行排序,然后打印其内容
Perhaps FileChannels would work for you:也许 FileChannels 会为你工作:
RandomAccessFile writer = new RandomAccessFile(file, "rw");
FileChannel channel = writer.getChannel();
ByteBuffer buff = ByteBuffer.wrap("Test write".getBytes(StandardCharsets.UTF_8));
channel.write(buff,5);//5 is the distance in the file
Lots of good examples on the web. web 上有很多很好的例子。
In your question I think you are taking acctNum as line number and you want to add a line at this line number in the file so you can do something like this.在你的问题中,我认为你正在将 acctNum 作为行号,你想在文件中的这个行号处添加一行,这样你就可以做这样的事情。
List<String> readLines = Files.readAllLines(path, StandardCharsets.UTF_8);
readLines.set(acctNum- 1, data);
Files.write(path, lines, StandardCharsets.UTF_8);
I assumed that you are using Java 7 or higher so I did acctNum-1
because in Java 7 or higher version line number starts with 1 in rest it starts with 0 so you can change to acctNum
.我假设您使用的是 Java 7 或更高版本,所以我执行了acctNum-1
,因为在 Java 7 或更高版本中,行号以 1 开头,在 rest 中,它以 0 开头,因此您可以更改为acctNum
。
Reference: List set() and NIO参考: 列表 set()和NIO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.