java_生成並打開本地html文件


 

//生成文件:

/**
* 讀取本地文件,並寫入StringBuffer ,以csv文件為例

*builders = new StringBuilders("<html><head><script>......<body>");調用方法前寫入頁面的開始標簽,方法結束后append頁面的結束標簽

*/
private static void csvToCache(File csv,StringBuilder builders,StringBuilder jsons){
  try {
    String encoding="GBK";
    DataInputStream in=new DataInputStream(new FileInputStream(csv));
    BufferedReader br = new BufferedReader(new InputStreamReader(in,encoding));
    String line = "";

    int index = 1;//定義序號

    int row=1;

    while ((line = br.readLine()) != null) //讀取到的內容給line變量
    {
      //do something;

      if(row==1) {//跳過第一行表頭
        row++;
        continue;
      }

      String contents[] = line.trim().split(",");//此處csv文件中每行的數據是按逗號分隔的

      for(int i=1;i<contents.length;i++) {

        //添加到相應的StringBuilder里面,builders.append("contents[i]");若有需要可使用index設置標識

      }

      index++;
    }
    br.close();
  } catch (Exception e) {
    System.out.println("csvToCache is error !");
    e.printStackTrace();
  }
}

/**

*根據StringBuffer的內容生成本地文件

*/

public static void createFile(StringBuilder builders,String url) {

  try{

    File file = new File(url);

    if(file.exists()) {//刪除原來的舊文件

      file.delete();

    }

    PrintStream printStream = new PrintStream(new FileOutputStream(file));

    printStream.println(builders.toString());

  }catch(FileNotFoundException e){

    e.printStackTrace();

  }

}

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

//打開文件:

//第一種: Object獲取項目中的properties

InputStream in = Object. class .getResourceAsStream( "/com/demo/conf.properties" );

//第二種: 直接獲得本地配置文件properties

FileInputStream in = new FileInputStream("D:\\work\\demo\\conf.properties"); 

//加載properties文件

Properties prop =  new  Properties();

prop.load(in);

//從配置文件中獲取 頁面的位置,此處url為網頁的絕對路徑並解決亂碼,如:d:/demo/index.html

 String url = new String(prop.getProperty( "csv_url" ).trim().getBytes("ISO-8859-1"),"gbk");

//根據url打開網頁

private static void browse(String url) throws ClassNotFoundException, IllegalAccessException,
      IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,NoSuchMethodException {
  String osName = System.getProperty("os.name", "");
  if (osName.startsWith("Windows")) {
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
  } else if (osName.startsWith("Mac OS")) {
    Class fileMgr = Class.forName("com.apple.eio.FileManager");
    Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
    openURL.invoke(null, new Object[] { url });
  } else { //  Unix or Linux
    String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
    String browser = null;
    for (int count = 0; count < browsers.length && browser == null; count++)
      if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
      browser = browsers[count];
    if (browser == null)
      throw new NoSuchMethodException("Could not find web browser");
    else
      Runtime.getRuntime().exec(new String[] { browser, url });
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM