[英]How to split the large size .txt file data into small portion and insert into database?
Below is my code to read and split the text file content. 下面是我的代码,用于读取和拆分文本文件内容。
try {
br = new BufferedReader(new FileReader("F:\\Test.txt"));
final char[] cbuf = new char[2048];
final int length = br.read(cbuf);
cbuf[length] = '@';
String packet = new String(cbuf, 0, length + 1);
final String[] splitedPacket=packet.split("@");
for(int i=0;i<splitedPacket.length;i++)
{
if(splitedPacket[i].contains("POS"))
{
System.out.println(splitedPacket[i]+"@");
preparedstatement=connection.prepareStatement("insert into inserttextfile([file]) values(?)");
preparedstatement.setString(1, splitedPacket[i]+"@");
preparedstatement.executeUpdate();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
It was working fine in my small size .txt file, but if I use more than 30 MB text file I get array index out of bound exception. 在我的.txt小文件中,它工作正常,但是如果我使用30 MB以上的文本文件,则数组索引超出绑定范围。
So how to split and read this kind of .txt file and insert into database? 那么,如何拆分和读取这种.txt文件并插入数据库呢? (particularly expecting for split the large size file(ex. like 5 MB from 30 MB) and insert into database) Please guide me in this issue.
(尤其是希望将大文件分开(例如从30 MB中提取5 MB)并插入数据库中)。
Your doing it the hard way by not using readLine 您不使用readLine很难做到这一点
try {
FileInputStream fis = new FileInputStream("F:\\Test.txt");
reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
while(line != null){
//process your line here, it's just a String...
line = reader.readLine();
}
} catch (FileNotFoundException ex) {
...
} catch (IOException ex) {
try (Scanner read = new Scanner(new File("/tmp/datafile.txt"));) {
read.useDelimiter("@");
while (read.hasNext()) {
String splitedPacket = read.next();
System.out.println(splitedPacket);
// Perform DB Operation
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.