Skip to content

Instantly share code, notes, and snippets.

@ShubhamS32
Created April 5, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShubhamS32/8508d20306bfbd77a4845912306d2f44 to your computer and use it in GitHub Desktop.
Save ShubhamS32/8508d20306bfbd77a4845912306d2f44 to your computer and use it in GitHub Desktop.
The following program explains how read /manipulate the files inside the jar file
package docker;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
public class docker {
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
*
* @author Shubham Shah
*
*/
System.out.println("Welcome to th3Javacode");
docker d = new docker();
try {
InputStream stream = docker.class.getResourceAsStream("/resources/abc.txt");
if(stream==null)
{
System.out.println("Error");
stream = docker.class.getClassLoader().getResourceAsStream("index.html");
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
System.out.println(System.getProperty("user.dir"));
File theDir = new File("resourcesss");
if (!theDir.exists()) {
System.out.println("creating directory: " + theDir.getName());
boolean result = false;
try{
theDir.mkdir();
result = true;
}
catch(SecurityException se){
//handle it
se.printStackTrace();
}
if(result) {
System.out.println("DIR created");
}
}
String st;
File file2 = new File(System.getProperty("user.dir")+"/resourcesss/index.html");
//Create the file
if (file2.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
//Write Content
FileWriter writer = new FileWriter(file2);
while ((st = br.readLine()) != null)
{ System.out.println(st);
writer.write(st);
}
writer.close();
String a="file:///"+System.getProperty("user.dir")+"/resourcesss/index.html";
URI myUri = new URI (a);
System.out.println("uri"+myUri);
Desktop.getDesktop().browse(myUri);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<!doctype html>
<head>
</head>
<body>
<h1>Hello World</h1>
<h2>Shubham Shah</h2>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment