如何通过Java判断一个txt文件是Windows格式还是Unix格式文件
How to judge a txt file is a Windows format or Unix format file by using Java
使用SQL*Loader加载数据到数据库,我们应该知道源数据的格式是Windows或Unix。
因此,我们如何使用JAVA获取文本文件的格式?如果我们能得到格式,我们应该在SQL*Loader ctl文件中定义格式参数。
我找到了解决这个问题的方法。这是代码仅供参考:
public static String getFileType(String filePath){
String fileType ="";
int count=0;
FileInputStream in=null;
try{
in =new FileInputStream(filePath);
StringBuffer systemFormat = new StringBuffer();
int i = 0;
while((count=in.read())!=-1){
//CR: ASCII: 13
if(count == 13){
systemFormat.append(String.valueOf(count));
i++;
}
//LF: ASCII: 10
if(count == 10){
systemFormat.append(String.valueOf(count));
i++;
}
if(i == 2) break;
}
if(systemFormat.toString().contains("1313")){
fileType = "Mac";
System.out.println("It's a Mac format file");
}else if(systemFormat.toString().contains("1310")){
fileType = "Windows";
System.out.println("It's a Windows format file");
}else if(systemFormat.toString().contains("1010")){
fileType = "Unix";
System.out.println("It's a Unix format file");
}
}catch(FileNotFoundException e) {
util.LogWriter.log(e);
}catch(IOException e) {
util.LogWriter.log(e);
}
return fileType;
}
使用SQL*Loader加载数据到数据库,我们应该知道源数据的格式是Windows或Unix。
因此,我们如何使用JAVA获取文本文件的格式?如果我们能得到格式,我们应该在SQL*Loader ctl文件中定义格式参数。
我找到了解决这个问题的方法。这是代码仅供参考:
public static String getFileType(String filePath){
String fileType ="";
int count=0;
FileInputStream in=null;
try{
in =new FileInputStream(filePath);
StringBuffer systemFormat = new StringBuffer();
int i = 0;
while((count=in.read())!=-1){
//CR: ASCII: 13
if(count == 13){
systemFormat.append(String.valueOf(count));
i++;
}
//LF: ASCII: 10
if(count == 10){
systemFormat.append(String.valueOf(count));
i++;
}
if(i == 2) break;
}
if(systemFormat.toString().contains("1313")){
fileType = "Mac";
System.out.println("It's a Mac format file");
}else if(systemFormat.toString().contains("1310")){
fileType = "Windows";
System.out.println("It's a Windows format file");
}else if(systemFormat.toString().contains("1010")){
fileType = "Unix";
System.out.println("It's a Unix format file");
}
}catch(FileNotFoundException e) {
util.LogWriter.log(e);
}catch(IOException e) {
util.LogWriter.log(e);
}
return fileType;
}