// 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