Java 中从文件读取直到特定字符

发布于 2024-11-25 17:39:51 字数 4240 浏览 0 评论 0原文

我有一个包含 sql 查询的文本文件。
每个查询都以“;”结尾。
我想执行这些查询。
这是我的问题;我想读取文件直到“;”然后执行我读过的查询。
我可以读取并执行一行查询,但无法读取超过一行的整个查询。

这是我写的代码;

try {
        String komut = "";
        BufferedReader bf = new BufferedReader(new FileReader("C:\\\\Users\\\\AhmetEmre\\\\Downloads\\\\text.txt"));

        while ((komut = bf.readLine()) != null) {
           if (komut.length() != 0) {
                if (komut.charAt(komut.length() - 1) == ’;’) {
                    komutVektoru.add(komut);
                    komut = "";
                } else {
                    komut += komut;
                }
           }
        }

文本文件中的示例;

**INSERT INTO tb_hukuk_ihbar ( id, hizmet_id, create_user_id, mektup_pdf, mektup_no, mektup_pdf_sayfa_no, ihbar_donemi)
VALUES
( 3672961, 1244494, 566, './FESIH20110721-123-001', '1107/001-003672961', 1, '201107');**
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 55367968, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 34811016, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 53849639, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 40120622, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 49865422, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 51456657, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 41151378, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 33450635, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 37954783, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 56885453, '94.6');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 56893779, '86.5');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 36398959, '14');
**INSERT INTO tb_hukuk_ihbar ( id, hizmet_id, create_user_id, mektup_pdf, mektup_no, mektup_pdf_sayfa_no, ihbar_donemi)
VALUES
( 3672962, 2458406, 566, './FESIH20110721-123-001', '1107/001-003672962', 2, '201107');**
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 53217996, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 51120970, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 36684544, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 40994810, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 38081806, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 49433813, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 35098768, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 30013966, '22');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 27578939, '22.85');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 28833729, '22');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 31258381, '18');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 55709156, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 33770763, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 32499838, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 39801860, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 56882759, '81.7');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 56942137, '98.7');

I have a text file that include sql queries.
Each query ends with ";".
I want to execute these queries.
Here is the my problem; i want to read the file until ";" and then execute the query that i have read.
I can read and execute the one-line queries but i can't read whole query that have more than one-line.

Here is the code that i wrote;

try {
        String komut = "";
        BufferedReader bf = new BufferedReader(new FileReader("C:\\\\Users\\\\AhmetEmre\\\\Downloads\\\\text.txt"));

        while ((komut = bf.readLine()) != null) {
           if (komut.length() != 0) {
                if (komut.charAt(komut.length() - 1) == ’;’) {
                    komutVektoru.add(komut);
                    komut = "";
                } else {
                    komut += komut;
                }
           }
        }

Example from text file;

**INSERT INTO tb_hukuk_ihbar ( id, hizmet_id, create_user_id, mektup_pdf, mektup_no, mektup_pdf_sayfa_no, ihbar_donemi)
VALUES
( 3672961, 1244494, 566, './FESIH20110721-123-001', '1107/001-003672961', 1, '201107');**
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 55367968, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 34811016, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 53849639, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 40120622, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 49865422, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 51456657, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 41151378, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 33450635, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 37954783, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 56885453, '94.6');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 56893779, '86.5');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672961, 36398959, '14');
**INSERT INTO tb_hukuk_ihbar ( id, hizmet_id, create_user_id, mektup_pdf, mektup_no, mektup_pdf_sayfa_no, ihbar_donemi)
VALUES
( 3672962, 2458406, 566, './FESIH20110721-123-001', '1107/001-003672962', 2, '201107');**
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 53217996, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 51120970, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 36684544, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 40994810, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 38081806, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 49433813, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 35098768, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 30013966, '22');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 27578939, '22.85');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 28833729, '22');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 31258381, '18');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 55709156, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 33770763, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 32499838, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 39801860, '14');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 56882759, '81.7');
INSERT INTO tb_hukuk_ihbar_detay( ihbar_id, fatura_id, odenmeyen_miktar) VALUES( 3672962, 56942137, '98.7');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仅此而已 2024-12-02 17:39:51

您可以使用 java.util.Scanner文件并将 ; 指定为 分隔符

像这样的东西:

Scanner scanner = new Scanner(new File("input.sql"));
scanner.useDelimiter(";");

while(scanner.hasNext()) {
    System.out.println("SQL statement: " + scanner.next());
}

You can use a java.util.Scanner with a file and specify ; as a delimiter.

Something like this:

Scanner scanner = new Scanner(new File("input.sql"));
scanner.useDelimiter(";");

while(scanner.hasNext()) {
    System.out.println("SQL statement: " + scanner.next());
}
留一抹残留的笑 2024-12-02 17:39:51

只是不要按行读取而是按字符读取:

int ch;
StringBuilder sb = new StringBuilder();
while ((ch = bf.read()) >= 0) {
   if (ch == ';') {
       execute(sb.toString());
       sb.setLength(0);
   } else 
       sb.append((char)ch);
   }
}

Just don't read by line and read by char instead:

int ch;
StringBuilder sb = new StringBuilder();
while ((ch = bf.read()) >= 0) {
   if (ch == ';') {
       execute(sb.toString());
       sb.setLength(0);
   } else 
       sb.append((char)ch);
   }
}
枫以 2024-12-02 17:39:51

为什么要将字符串“komut”附加到其自身上?您将当前行与“到目前为止的 sql 命令”字符串混淆了。

您需要添加另一个变量:

    String query = "";
    while ((komut = bf.readLine()) != null) {
       if (komut.length() != 0) {
            if (komut.charAt(komut.length() - 1) == ’;’) {
                komutVektoru.add(query + "\n"+ komut);
                query = "";
            } else {
                query += komut;
            }
       }
    }

Why would you append the string "komut" to itself? You're mixing up your current line with your "sql command up to now" string.

You need to add another variable:

    String query = "";
    while ((komut = bf.readLine()) != null) {
       if (komut.length() != 0) {
            if (komut.charAt(komut.length() - 1) == ’;’) {
                komutVektoru.add(query + "\n"+ komut);
                query = "";
            } else {
                query += komut;
            }
       }
    }
呆° 2024-12-02 17:39:51
BufferedReader br = new BufferedReader(new FileReader("yourFile"));
String line;
StringBuilder query = new StringBuilder();

while( (line=br.readLine()) !=null)
{
     //You should also check if a line is a comment
    if(line.trim().endsWith(";"))
    {
        executeQuery( query.toString() );
        query = new StringBuilder();
    }else
    {
        query.append(line);
    }

}
BufferedReader br = new BufferedReader(new FileReader("yourFile"));
String line;
StringBuilder query = new StringBuilder();

while( (line=br.readLine()) !=null)
{
     //You should also check if a line is a comment
    if(line.trim().endsWith(";"))
    {
        executeQuery( query.toString() );
        query = new StringBuilder();
    }else
    {
        query.append(line);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文