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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6786708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:16:26  来源:igfitidea点击:

Reading from a File Until Specific Character in Java

javasqltext-files

提问by AhmetEmre90

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.

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

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');

采纳答案by Sergey Aslanov

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

为什么要将字符串“komut”附加到自身?您将当前行与“sql command up to now”字符串混淆。

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

回答by wjans

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

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

Something like this:

像这样的东西:

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

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

回答by Sergey Aslanov

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

回答by HectorLector

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

}