簡體   English   中英

如何使用 java 編輯文本文件中的特定行?

[英]How would I edit a specific line in a text file with java?

我正在編寫一個程序,允許用戶將最多 9999 個帳戶輸入到一個文本文件中,但是我遇到的問題是它們可以按任何順序放置,但我必須按順序打印它們。 這是我的代碼

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");
        }
    }
}

我的問題是,我怎樣才能得到它,以便當用戶輸入“55”時,它被打印到文本文件的第 55 行?

你可以這樣做:

1)創建一個 class 來存儲你的行(acctNum,lastName ..等)

2)在你的方法中,創建一個你創建的class的arraylist,對於給定的數字“n”,你的方法將解析所有行,如果acctNum小於“n”,你將使用此行創建一個新實例並且將其添加到您的 arraylist

3)您將使用 acctNum 對 arraylist 進行排序,然后打印其內容

也許 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

web 上有很多很好的例子。

在你的問題中,我認為你正在將 acctNum 作為行號,你想在文件中的這個行號處添加一行,這樣你就可以做這樣的事情。

    List<String> readLines = Files.readAllLines(path, StandardCharsets.UTF_8);
    readLines.set(acctNum- 1, data);
    Files.write(path, lines, StandardCharsets.UTF_8);

我假設您使用的是 Java 7 或更高版本,所以我執行了acctNum-1 ,因為在 Java 7 或更高版本中,行號以 1 開頭,在 rest 中,它以 0 開頭,因此您可以更改為acctNum
參考: 列表 set()NIO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM