Java 中从文件读取直到特定字符
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 java.util.Scanner文件并将
;
指定为 分隔符。像这样的东西:
You can use a java.util.Scanner with a file and specify
;
as a delimiter.Something like this:
只是不要按行读取而是按字符读取:
Just don't read by line and read by char instead:
为什么要将字符串“komut”附加到其自身上?您将当前行与“到目前为止的 sql 命令”字符串混淆了。
您需要添加另一个变量:
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:
考虑使用 TextScanner: http://www.java-tips.org/java-se-tips/java.util/scanning-text-with-java.util.scanner-3.html
Consider using TextScanner: http://www.java-tips.org/java-se-tips/java.util/scanning-text-with-java.util.scanner-3.html