Java_ generate and open a local HTML file

Source: Internet
Author: User
Tags stringbuffer

// generate file:

/**
* Read local file and write to StringBuffer, take CSV file for example

*builders = new StringBuilders ("

*/
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; //define serial number

int row=1;

while (line = Br.readline ()) = null) //Read the content to the line variable
{
      Do something;

if (row==1) {//Skip First row header
row++;
Continue
}

String contents[] = Line.trim (). Split (","); //The data for each row in the CSV file here is separated by commas

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

        Add to the corresponding StringBuilder inside, Builders.append ("contents[i]"), and if necessary, use index to set the identity

}

index++;
}
Br.close ();
} catch (Exception e) {
System.out.println ("Csvtocache is Error!");
E.printstacktrace ();
}
}

/**

* Generate local files based on StringBuffer content

*/

public static void CreateFile (StringBuilder builders,string URL) {

try{

File File = new file (URL);

if (file.exists ()) {//delete original old file

File.delete ();

}

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

Printstream.println (Builders.tostring ());

}catch (FileNotFoundException e) {

E.printstacktrace ();

}

}

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

// Open File:

The first type: Object gets the properties in the project

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

The second type: direct access to local configuration file properties

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

Load Properties File

Properties prop = new properties ();

Prop.load (in);

Gets the location of the page from the configuration file, where the URL is the absolute path to the Web page, such as: d:/demo/index.html

String url = prop.getproperty ("Csv_url"). Trim ();

Open Web page by 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});
}
}

Java_ generate and open a local HTML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.