0

Using SQL*Loader to load data to database, we should know the source data's format is Windows or Unix.

Therefor, how can we get the text file's format using JAVA? If we can get the format, we should define the format parameter in the SQL*Loader ctl file.

Lionel Yan
  • 101
  • 1
  • 4
  • I don't think you don't need to know which format that is. SQL\*Loader will happily accept both formats if I'm not mistaken. –  Oct 18 '16 at 09:07
  • Refer this link for how can we get the text file's format using [JAVA](http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character) – Karthik Oct 18 '16 at 09:22
  • @a_horse_with_no_name I have met the problem when load Windows format file by SQL\*Loader which is in Unix System. The SQL\*Loader parse the source file with mistake. So I want get the file's format using java directly and define it in the SQL\*Loader's ctl file. Then SQL\*Loader will deal it correctly. – Lionel Yan Oct 19 '16 at 01:58
  • @Karthik I don't need get the file's line-endings. I need deal a big source data file(more than 300M) with SQL\*Loader, so we can't read the source file line by line which performance is very bad. Thank you all the same. – Lionel Yan Oct 19 '16 at 02:07

1 Answers1

0

I have found a method to resolve this question. Here is the code FYI:

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;
}
Lionel Yan
  • 101
  • 1
  • 4