顯示具有 JAVA 標籤的文章。 顯示所有文章
顯示具有 JAVA 標籤的文章。 顯示所有文章

Java NIO逐行读文件并写文件


/**
 * Created by scott on 2019/7/19.
 */
public class test {
    public static void main(String args[]) throws Exception {
        int bufSize = 100;
        File fin = new File("C:\\Users\\User\\Desktop\\localhost_access_log.2019-07-16.txt");
        File fout = new File("C:\\Users\\User\\Desktop\\localhost_access_log.2019-07-16_AAAA.txt");

        FileChannel fcin = new RandomAccessFile(fin, "r").getChannel();
        ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);

        FileChannel fcout = new RandomAccessFile(fout, "rws").getChannel();
        ByteBuffer wBuffer = ByteBuffer.allocateDirect(bufSize);

        readFileByLine(bufSize, fcin, rBuffer, fcout, wBuffer);

        System.out.print("OK!!!");
    }

    public static void readFileByLine(int bufSize, FileChannel fcin, ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer) {
        String enterStr = "\n";
        try {
            byte[] bs = new byte[bufSize];
            StringBuffer strBuf = new StringBuffer("");
            while (fcin.read(rBuffer) != -1) {
                int rSize = rBuffer.position();
                rBuffer.rewind();
                rBuffer.get(bs);
                rBuffer.clear();
                String tempString = new String(bs, 0, rSize);
                int fromIndex = 0;
                int endIndex = 0;
                //查找换行符符号\n,如果找到了,则写文件,如果没有找到则继续读取
                while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
                    String line = tempString.substring(fromIndex, endIndex);
                    line = strBuf.toString() + line;
                    System.out.println(line);
                    ;

                    if (line.contains("1111111111111111")) {
                        writeFileByLine(fcout, line + "\n");
                    }

                    strBuf.delete(0, strBuf.length());
                    fromIndex = endIndex + 1;
                }

                if (rSize > tempString.length()) {
                    strBuf.append(tempString.substring(fromIndex, tempString.length()));
                } else {
                    strBuf.append(tempString.substring(fromIndex, rSize));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFileByLine(FileChannel fcout, String line) {
        try {
            fcout.write(ByteBuffer.wrap(line.getBytes()), fcout.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java 8 中的 Streams API

  public static void main(String[] args) {
        List<OrderInfo> orderList = new ArrayList<>();

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(null);


        OrderInfo orderInfo2 = new OrderInfo();
        orderInfo2.setOrderNo("123");

        OrderInfo orderInfo3 = new OrderInfo();
        orderInfo3.setOrderNo("222");


        orderList.add(orderInfo);
        orderList.add(orderInfo2);
        orderList.add(orderInfo3);

//        ---------------------------------------------------------------

        orderList = orderList.stream()
                .filter(                 //篩選
//                        o -> o.getOrderNo() != null
                        o -> {
                            if (o.getOrderNo() != null) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                )
                .map(o -> {
//                    o.setOrderNo(o.getOrderNo() + "----");
                    return o;
                })
                .sorted((o1,o2) -> {    //設值
                    Integer oo1 = Integer.parseInt(o1.getOrderNo());
                    Integer oo2 = Integer.parseInt(o2.getOrderNo());
                    return oo2.compareTo(oo1);
                })
                .limit(1)               //幾個之前
                .collect(Collectors.toList());



//        走訪
        orderList.stream().forEach(o -> System.out.println(o.getOrderNo()));
        orderList.parallelStream().forEach(o -> System.out.println(o.getOrderNo()));    //有順序的


    }

[JAVA] 圖檔搜尋程式


/* 把電腦裡的圖檔印出來
 * 再把檔名存到一個檔案裡
 * 找到重複的檔案,並詢問是否要刪除
 */
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class fileImageSearch {
 // 成員區
 int sum = 0;
 long start = 0l;
 long end = 0l;
 String str = null;
 BufferedReader br = null;
 BufferedWriter bw = null;
 boolean store = false;
 SimpleDateFormat sdf = null;
 LinkedList al = new LinkedList<>();

[JAVA] NIO讀檔


/**
 * 讀檔 .txt
 **/
public static String readTxt(String File, String Coding) {
 String rtn = "";
 int i = 0;

 try {
  // 判斷檔案是否存在
  File file = new File(File);
  if (file.exists() != false && file.isFile() != false) {
   Path path = Paths.get(File);
   // 讀出文字檔
   rtn = new String(Files.readAllBytes(path), Coding);
  }

 } catch (Exception e) {
  e.printStackTrace();
 }
 return rtn;
}