需求:将一堆的 .sql 文件插入到oracle中
遇到的问题:.sql的文件中有很多的地方有commit和prompt标示,直接open是不能正确插入的,所以要用程序将其剃掉。
程序如下:
/**
* 写文件.
* @param filepath
* @param contents
*/
private static void writeFile(String filepath , String contents){
File file = new File(filepath);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
writer.write(contents);
writer.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取文件
* @param filePath
* @return
*/
private static String readFile(String filePath){
StringBuilder buffer = new StringBuilder("");
try {
File file = new File(filePath);
if (file.isFile()&&file.exists()) {
FileInputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in,"UTF-8");
BufferedReader br = new BufferedReader(reader);
String line = null;
while ((line = br.readLine()) != null) {
if (!(line.contains("commit") || line.contains("prompt "))) {
System.out.println(line);
//buffer.append(new String(line.getBytes(), "UTF-8")+"\n");
buffer.append(line+"\n");
}
}
br.close();
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
但是写出来的文件还是乱码,很是不知所措,查阅了相关文档和资料,还是不能解决这个乱码的问题,因为他源头的编码无法获知,所以才会这样。
无赖只能手动去修改出现!(line.contains("commit") || line.contains("prompt "))部分地方,修改完成之后,再去insert
倒也是成功了,但是回想到了一种方法,可是有点后悔,所以现在记录下来。
那就是:用一个编译器,editplus或是notepad++ 打开文件之后,设置好编码,然后在去操作文件用上面的方式就可以去掉sql文件中多余的commit和prompt了。
看来思考的不够!!!
需求二:
将一个大的文件,分割成不同的小块(小的文件)。
思路:1.总的文件数;2小文件读取的行数(个数);3.循环读取文件。
直接性的上干货:
/**
* 读取文件
* @param filePath:文件路径
* @param index:起始点
* @param count:读取多少个
* @return
*/
private static List<String> readFile(String filePath,int index,int count){
List<String> lineList = new ArrayList<String>();
try {
File file = new File(filePath);
if (file.isFile()&&file.exists()) {
FileInputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in,"UTF-8");
BufferedReader br = new BufferedReader(reader);
String line = null;
int point=0;
while ((line = br.readLine()) != null) {
if (!(line.contains("commit") || line.contains("prompt "))) {
point++;
if (index<point && point<=count) {
System.out.println("inex = "+index+",point = "+point+",count = "+count);
lineList.add((new String(line.getBytes(), "UTF-8")+"\n"));
}
}
}
br.close();
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return lineList;
}
/**
* 获得总行数。
* @return
*/
private static int getSum(String filePath){
int index = 0;
try {
File file = new File(filePath);
if (file.isFile()&&file.exists()) {
FileInputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in,"UTF-8");
BufferedReader br = new BufferedReader(reader);
String line = null;
while ((line = br.readLine()) != null) {
index++;
}
br.close();
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return index;
}
/**
* 写文件(3000行的去写).
* @param filepath
* @param contents
*/
private static void writeFile(String filepath , List<String> lineList){
File file = new File(filepath);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
for (int i = 0; i < lineList.size(); i++) {
writer.write(lineList.get(i));
}
writer.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
只是一个应用,觉得应该有用,就贴出来了、高手就不需要看了。。。