开发者

Reading from a File Until Specific Character in Java

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, 53开发者_StackOverflow849639, '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');


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


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


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


Consider using TextScanner: http://www.java-tips.org/java-se-tips/java.util/scanning-text-with-java.util.scanner-3.html


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜