lyphtesttest 常用ファイルの操作、検索。excelの操作  java

削除: del D:msworkspaceK1F_BATCHsrccommitsubishi_shokuhink1fatchJFKC011201.java コピー: echo F|xcopy D:msworkspaceK1F_BATCHsrccommitsubishi_shokuhink1fatchJFKC011201.java D:ースXXXK1F_BATCHsrccommitsubishi_shokuhink1fatchJFKC011201.java /y /r /i /f 対比: =IF(D3<>"",VLOOKUP(D3,B:C,2,0),"")

常用ソース: import java.io.File; import java.text.ParseException; import java.util.ArrayList; import java.lang.NullPointerException;

public class lyphtesttest {

 private static ArrayList<String> filelist = new ArrayList<String>();

 private static String filePathFrom = "D://xxxxXXX";

 private static String fileTo = "D://ms//workspace-20150909LYZ";

 private static String fileToxxx = "D:\ms\workspace-20150909LYZ";

 private static String filePathFromxxx = "D:\ms\workspace-20150909";

 /**   * @param args   * @throws ParseException   */  public static void main(String[] args) throws ParseException {

  //System.out.println(subStringLenth("1234567", 0));   //return;

  getFiles(filePathFrom);  }

 private static String subStringLenth(String str, int intEnd) {   String result = "";   if (str == null || str.trim().isEmpty()) {    return result;   }   if (str.length() >= intEnd) {    return str.substring(0, intEnd);   }   return str;  }

 static void getFiles(String filePath) {   File root = new File(filePath);   File[] files = root.listFiles();   for (File file : files) {    if (file.isDirectory()) {

    getFiles(file.getAbsolutePath());     filelist.add(file.getAbsolutePath());     //    System.out.println("表示" + filePath + "サブフォルダとファイル" + file.getAbsolutePath());    }    else {     String fileName = file.getName();     String 拡張子 = checkFile(fileName);     if (!拡張子.isEmpty()) {      System.out.println(fileName + " " + file.getAbsolutePath() + " " + 拡張子);      // copy命令の作成      //     copyPrint(filePath, file.getAbsolutePath());     }    }   }  }

 private static void copyPrint(String pathTitle, String pathFromAll) {   String copy = "echo F|xcopy ";   String parCopy = " /y /r /i /f";

  String copyToAll = pathFromAll.replace(filePathFromxxx, fileToxxx);

  System.out.println(copy + pathFromAll + " " + copyToAll + " " + parCopy);  }

 private static String checkFile(String fileName) {

  if (fileName.endsWith(".java")) {    return "java";   }   if (fileName.endsWith(".js")) {    return "js";   }   if (fileName.endsWith(".jsp")) {    return "jsp";   }   if (fileName.endsWith(".css")) {    return "css";   }   if (fileName.endsWith(".xml")) {    return "xml";   }   if (fileName.endsWith(".clg2")) {    return "clg2";   }   if (fileName.endsWith(".ifd")) {    return "ifd";   }   if (fileName.endsWith(".properties")) {    return "properties";   }   if (fileName.endsWith(".dicon")) {    return "dicon";   }   if (fileName.endsWith(".scd")) {    return "scd";   }   if (fileName.endsWith(".scp")) {    return "scp";   }   if (fileName.endsWith(".classpath")) {    return "classpath";   }

  return "";  }

}

import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map;

public class FileMakeCopy {

 private static Map<String, String> ファイル読込;  private static Map<String, String> ファイル作成;

 static List<String> getFiles(String filePath, String fileToTmp, List<String> log内容) {

 File root = new File(filePath);  File[] files = root.listFiles();  log内容.add(filePath + ": start......");  for (File file : files) {  if (file.isDirectory()) {  String 新たなinput = file.getAbsolutePath();  String 新たなoutput = fileToTmp + "\" + file.getAbsolutePath().replace(filePath, "");  getFiles(新たなinput, 新たなoutput, log内容);  } else {  String fileName = file.getName();  String 拡張子 = 拡張子の判定(fileName);

 if (!拡張子.isEmpty()) {  try {  元ファイル読み込む(filePath, file.getAbsolutePath());  新たなファイル作成(file.getName(), fileToTmp);  } catch (Exception e) {  log内容.add(file.getAbsolutePath() + ":処理は失敗");  log内容.add(e.toString());  continue;  }  log内容.add(file.getAbsolutePath() + ":成功");  } else {  log内容.add("ファイルのタイプはおかしい:" + file.getAbsolutePath());  }  }  }  log内容.add(filePath + " end......");  return log内容;  }

 private static void 元ファイル読み込む(String pathTitle, String pathFromAll) throws IOException {

 ファイル読込 = new HashMap<String, String>();  File file = new File(pathFromAll);  BufferedReader br = new BufferedReader(new FileReader(file));  String str = br.readLine();  int レコード計数 = 0;  while (str != null) {  ファイル読込.put(String.valueOf(レコード計数), str);  レコード計数++;  str = br.readLine();  }  br.close();  }

 private static void 新たなファイル作成(String ファイル名, String fileToTmp) throws Exception {

 File file = new File(fileToTmp + "\" + ファイル名);  if (ファイル存在チェック(file)) {

 int 読込データ数 = ファイル読込.size();  if (読込データ数 == 0) {  return;  }  ファイル作成 = new HashMap<String, String>();  int 作成計数 = 0;  for (int 読込計数 = 0; 読込計数 < 読込データ数; 読込計数++) {

 ファイル作成.put(String.valueOf(作成計数), ファイル読込.get(String.valueOf(読込計数)));  作成計数++;  }  }  PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));  for (int i = 0; i < ファイル作成.size(); i++) {  pw.println(ファイル作成.get(String.valueOf(i)));  }  pw.close();  }

 private static boolean ファイル存在チェック(File file) throws Exception {  if (file.exists()) {  if (file.isFile() && file.canWrite()) {  return true;  } else {  throw new Exception("ファイルを使用中.......");  }  }  file.getParentFile().mkdirs();  file.createNewFile();  return true;  }

 private static String 拡張子の判定(String fileName) {

 if (fileName.endsWith(".txt")) {  return "txt";  }  if (fileName.endsWith(".csv")) {  return "csv";  }  return "";  } }

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.List; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.JToolBar; import javax.swing.ScrollPaneConstants; public class FormDemo extends JFrame {  private static final long serialVersionUID = 1L;  JTextPane textPane = new JTextPane();  JLabel statusBar = new JLabel();  JFileChooser filechooserInput = new JFileChooser();  JFileChooser filechooserOutput = new JFileChooser();  String filePathFrom = "";  String filePathTo = "";  public FormDemo() {  Action[] actions = { new OpenActionFile(), new OpenActionFolder(), new StartAction(), new ClearAction(),  new ExitAction() };  setJMenuBar(createJMenuBar(actions));  Container container = this.getContentPane();  container.add(createJToolBar(actions), BorderLayout.NORTH);  container.add(textPane, BorderLayout.CENTER);  container.add(statusBar, BorderLayout.SOUTH);  int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;  int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;  JScrollPane jsp = new JScrollPane(textPane, v, h);  container.add(jsp);  setSize(600, 500);  setLocationRelativeTo(null);  setTitle("MainForm");  setVisible(true);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  }

 private JMenuBar createJMenuBar(Action[] actions) {  JMenuBar menubar = new JMenuBar();  return menubar;  }

 private JToolBar createJToolBar(Action[] actions) {  JToolBar toolBar = new JToolBar();  for (int i = 0; i < actions.length; i++) {  JButton bt = new JButton(actions[i]);  bt.setRequestFocusEnabled(false);  toolBar.add(bt);  }  return toolBar;  }

 class OpenActionFile extends AbstractAction {  public OpenActionFile() {  super("input");  }

 public void actionPerformed(ActionEvent e) {  filechooserInput.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  int i = filechooserInput.showOpenDialog(FormDemo.this);  if (i == JFileChooser.APPROVE_OPTION) {  filechooserInput.getSelectedFile();  filePathFrom = filechooserInput.getSelectedFile().getPath();  textPane.setText(textPane.getText() + " input:" + filePathFrom);  }  }  }

 class OpenActionFolder extends AbstractAction {  public OpenActionFolder() {  super("output");  }

 public void actionPerformed(ActionEvent e) {

 filechooserOutput.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  int i = filechooserOutput.showOpenDialog(FormDemo.this);  if (i == JFileChooser.APPROVE_OPTION) {  filechooserOutput.getSelectedFile();  filePathTo = filechooserOutput.getSelectedFile().getPath();  textPane.setText(textPane.getText() + " output:" + filePathTo);  }  }  }

 class StartAction extends AbstractAction {  public StartAction() {  super("start...");  }

 public void actionPerformed(ActionEvent e) {

 if ("".equals(filePathFrom)) {  JOptionPane.showMessageDialog(null, "input フォルダを設定してください。");  return;  }  if ("".equals(filePathTo)) {  JOptionPane.showMessageDialog(null, "output フォルダを設定してください。");  return;  }  List<String> log内容 = new ArrayList<String>();  FileMakeCopy.getFiles(filePathFrom, filePathTo, log内容);

 for (String log : log内容) {  textPane.setText(textPane.getText() + " " + log);  }  }  }

 class ClearAction extends AbstractAction {  public ClearAction() {  super("clear");  }

 public void actionPerformed(ActionEvent e) {  textPane.setText("");  filePathFrom = "";  filePathTo = "";  }  }

 class ExitAction extends AbstractAction {  public ExitAction() {  super("exit");  }

 public void actionPerformed(ActionEvent e) {  System.exit(0);  }  }

 public static void main(String[] args) {  new FormDemo();  } }

原文地址:https://www.cnblogs.com/liuyunpiaohe/p/4846513.html