temp

package com.csf.myproject.core.write;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * write to txt file
 * @author fenglei.ma March 21, 2018 at 11:24:28 AM
  */ 
public  class TestWrite {

    public static void main(String[] args) {
        System.out.println("----------------------start----------------------");
        try {
            String path = "C:\\Users\\fenglei.ma\\Desktop\\Hexun service deployment\\test\\" ;
            String pathFile = path + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".txt";

            File file = new File(pathFile);
             // Create the directory where the target file does not exist 
            if (! file.getParentFile().exists()) {
                 if (file.getParentFile().mkdirs()) {
                    System.out.println( "【 " + file.getParentFile() + " ] The directory does not exist, it has been created" );
                }
            }
            if (file.createNewFile()) {
                System.out.println( "[ " + file + " ] The file has been created" );
            }
            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));

            // Write the file 
            out.write("<<entity>>" .getBytes());
            out.write("\r\n".getBytes());
            out.write( "Name related words" .getBytes());
            out.write("\r\n".getBytes());
            
            for (int i = 0; i < 6; i++) {
                out.write((("datas数据".trim() + i) + " , " + "完成" + i).getBytes());
                out.write("\r\n".getBytes());// 换行
            }
            out.write("\r\n".getBytes());// 换行
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        System.out.println("----------------------end----------------------");
    }

}

----------------------------------------------------------------------------------------

List<String> sids = Lists.newArrayList();
try{
   InputStreamReader isr = new InputStreamReader( new FileInputStream( new File("C:\\Users\\fenglei.ma\\Desktop\\China Merchants Industry Chain\\Second Phase Optimization\\Data Repair.txt")), "UTF-8 " );
   BufferedReader br = new BufferedReader(isr);
   String line = "";
   while((line = br.readLine())!= null){
      sids.add(line);
   }
   sids = sids.stream().distinct().collect(Collectors.toList());
}catch(Exception e){
   e.printStackTrace ();
}

package com.csf.myproject.core.csv;

import java.io.File;
import java.nio.charset.Charset;

import com.csvreader.CsvReader;

/**
 * csv file reading
 *
 * @author fenglei.ma March 21, 2018 at 11:35:54 AM
  */ 
public  class CsvTest {

    public static void main(String[] args) throws Exception {

        String path = "C:\\Users\\fenglei.ma\\Desktop\\Hexun service deployment\\test\\simple test file.csv" ;
        String charset = getJavaEncode(path);

        CsvReader reader = new CsvReader(path, ',', Charset.forName(charset));
        reader.setSafetySwitch(false);
        reader.readHeaders(); // Skip the header 

        System.out.println( "dt auth title content url htid" );
         while (reader.readRecord()) { // Read the header data line by line 
            try {
                String[] lineData = reader.getValues();

                String dt = lineData[0];
                String auth = lineData[1];
                String title = lineData[2];
                String content = lineData[3];
                String url = lineData[4];
                String htid = lineData[5];

                System.out.println(dt + "  " + auth + " " + title + "  " + content + "  " + url + "  " + htid);

            } catch (Exception e) {

            }
        }
    }

    /**
     * Get the encoding of the file
     *
     * @param filePath file path
     * @return the encoding of the file
      */ 
    public  static String getJavaEncode(String filePath) {
        BytesEncodingDetect s = new BytesEncodingDetect();
        String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(new File(filePath))];
        return fileCode;
    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324832023&siteId=291194637