Java file not found - java

I am calling a file glassShader.vert from the following method and it gives me FileNotFoundException error
The complicated issue is that the class GLGridRenderer that contains this method lies in the directory GridLogin which is in turn inside the package com.jasfiddle.AmazingInterface
So to address the directory, it would be com.jasfiddle.AmazingInterface.GridLogin
But I don't know how to call shader.vert which is inside GridLogin
public static String readShaderFile(String filepath) throws IOException {
FileInputStream stream = new FileInputStream(new File(filepath));
try{
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
return Charset.defaultCharset().decode(bb).toString();
}
finally{
stream.close();
}
}

other than raw you can also use asset folder see link ....
try {
// get input stream for text
InputStream is = getAssets().open("text.txt");
// check size
int size = is.available();
// create buffer for IO
byte[] buffer = new byte[size];
// get data to buffer
is.read(buffer);
// close stream
is.close();
}
catch (IOException ex) {
return;
}

Files that you want to read should not be put in a package. They should be packaged as resources or assets. For instance, with a data file, put it in the res/raw folder and give it a legal resource name. Then you can open an input stream if you have a Context (such as your Activity class or a View class).
InputStream stream = context.getResources().openRawResource(R.raw.filepath);
(This would be if you named the file res/raw/filepath.dat. You'd probably want a more meaningful name. If you want the name to be a variable, then you can obtain the resource ID using:
int resId = context.getResources.getIdentifier(filepath, "raw", context.getPackageName());

Related

FileOutputStream, directory

this is my directory structure
Inside the server I have the following code for saving a file that gets sent from the client
fileName = reader.readLine();
DataInputStream dis = null;
try {
dis = new DataInputStream(csocket.getInputStream());
FileOutputStream fos = new FileOutputStream(fileName);
buffer = new byte[4096];
int fileSize = 15123;
int read = 0;
int totalRead = 0;
int remaining = fileSize;
while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
} catch (IOException e) {
}
break;
I'm wondering how I would go about saving the file within the xml folder? I've tried using getClass().getResource and such but nothing seems to work.
fileName is just a simple string containing the name of the file, not a path or anything.
I get the correct path using this code:
File targetDir = new File(getClass().getResource("xml").getPath());
File targetFile = new File(targetDir, fileName);
targetFile.createNewFile();
System.out.println(targetFile.getAbsolutePath());
dis = new DataInputStream(csocket.getInputStream());
FileOutputStream fos = new FileOutputStream(targetFile.getAbsolutePath(), false);
But it still won't save it there...
The best way is to receive explicitly the target path for storing files, either through a .properties file or a command-line argument. In this way, you make your program flexible to be installed and adapted in different environments.
But if you wish your program to assume the target directory automatically, the best option is to set a relative path before creating the FileOutputStream, as long as you start your program always from the same path:
File targetDir=new File("xml");
File targetFile=new File(targetDir, fileName);
FileOutputStream fos = new FileOutputStream(targetFile);
This will work assuming the program is started from server as current directory.
Update
Other minor suggestions about your program:
Never base the exit condition of the loop on a hard-coded file size, because it is not possible to know it a priori. Instead, check explicitly if the value returned by read is less than 0 => that means End Of File reached.
Consequently, do not bother to calculate the exact amount of data to get through a call to read. Just enter the buffer size, because you are setting a maximum data size.
Never let exceptions catched without a proper treatment: If you know how to make your program recover, enter a proper code into the catch block. Otherwise, you'd better not catch them: Declare them in the throws clause and let them be propagated to the caller.
Always create stream resources through the try-with-resources instruction, to ensure they got closed at the end:
try (FileOutputStream fos = new FileOutputStream(...))
{
// ... use fos...
}
Save unnecessary instructions: If you don't care about if the file already exists on the filesystem or not, don't call createNewFile. But if you care, check the returned value and bifurcate consequently.
I try to create file and it does created but not at ProjectName\src\com\company\xml but in ProjectName\out\production\ProjectName\com\company\xml
my code:
File targetDir = new File(this.getClass().getResource("xml").getPath());
// get the parent of the file
String parentPath = targetDir.getParent( );
String fileName="xml/name.txt";
//do something
File targetFile = new File(parentPath, fileName);
targetFile.createNewFile();
Just pay attention that after compilation you will try to save it into a jar file and it a complicated thing to do.
usually you need to save file into file outside from your jar(separate in the root) like this:

How to make a copy of a file containing images and text using java

I have some word documents and excel sheets which has some images along with the file text content. I want to create a copy of that file and keep it at a specific location. I tried the following method which is creating file at specified location but the file is corrupted and cannot be read.
InputStream document = Thread.currentThread().getContextClassLoader().getResourceAsStream("upgradeworkbench/Resources/Upgrade_TD_Template.docx");
try {
OutputStream outStream = null;
Stage stage = new Stage();
stage.setTitle("Save");
byte[] buffer= new byte[document.available()];
document.read(buffer);
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialFileName(initialFileName);
if (flag) {
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Microsoft Excel Worksheet", "*.xls"));
} else {
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Microsoft Word Document", "*.docx"));
}
fileChooser.setTitle("Save File");
File file = fileChooser.showSaveDialog(stage);
if (file != null) {
outStream = new FileOutputStream(file);
outStream.write(buffer);
// IOUtils.copy(document, outStream);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
Can anyone suggest me any different ways to get the proper file.
PS: I am reading the file using InputStream because it is inside the project jar.
PPS: I also tried Files.copy() but it didnt work.
I suggest you never trust on InputStream.available to know the real size of the input, because it just returns the number of bytes ready to be immediately read from the buffer. It might return a small number, but doesn't mean the file is small, but that the buffer is temporarily half-full.
The right algorithm to read an InputStream fully and write it over an OutputStream is this:
int n;
byte[] buffer=new byte[4096];
do
{
n=input.read(buffer);
if (n>0)
{
output.write(buffer, 0, n);
}
}
while (n>=0);
You can use the Files.copy() methods.
Copies all bytes from an input stream to a file. On return, the input stream will be at end of stream.
Use:
Files.copy(document, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
As the class says, the second argument is a Path, not a File.
Generally, since this is 2015, use Path and drop File; if an API still uses File, make it so that it uses it at the last possible moment and use Path all the way.

How to copy files out of the currently running jar

I have a .jar that has two .dll files that it is dependent on. I would like to know if there is any way for me to copy these files from within the .jar to a users temp folder at runtime. here is the current code that I have (edited to just one .dll load to reduce question size):
public String tempDir = System.getProperty("java.io.tmpdir");
public String workingDir = dllInstall.class.getProtectionDomain().getCodeSource().getLocation().getPath();
public boolean installDLL() throws UnsupportedEncodingException {
try {
String decodedPath = URLDecoder.decode(workingDir, "UTF-8");
InputStream fileInStream = null;
OutputStream fileOutStream = null;
File fileIn = new File(decodedPath + "\\loadAtRuntime.dll");
File fileOut = new File(tempDir + "loadAtRuntime.dll");
fileInStream = new FileInputStream(fileIn);
fileOutStream = new FileOutputStream(fileOut);
byte[] bufferJNI = new byte[8192000013370000];
int lengthFileIn;
while ((lengthFileIn = fileInStream.read(bufferJNI)) > 0) {
fileOutStream.write(bufferJNI, 0, lengthFileIn);
}
//close all steams
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
System.out.println(e);
return false;
}
My main problem is getting the .dll files out of the jar at runtime. Any way to retrieve the path from within the .jar would be helpful.
Thanks in advance.
Since your dlls are bundeled inside your jar file you could just try to acasses them as resources using ClassLoader#getResourceAsStream and write them as binary files any where you want on the hard drive.
Here is some sample code:
InputStream ddlStream = <SomeClassInsideTheSameJar>.class
.getClassLoader().getResourceAsStream("some/pack/age/somelib.dll");
try (FileOutputStream fos = new FileOutputStream("somelib.dll");){
byte[] buf = new byte[2048];
int r;
while(-1 != (r = ddlStream.read(buf))) {
fos.write(buf, 0, r);
}
}
The code above will extract the dll located in the package some.pack.age to the current working directory.
Use a class loader that is able to locate resources in this JAR file. Either you can use the class loader of a class as Peter Lawrey suggested, or you can also create a URLClassLoader with the URL to that JAR.
Once you have that class loader you can retrieve a byte input stream with ClassLoader.getResourceAsStream. On the other hand you just create a FileOutputStream for the file you want to create.
The last step then is to copy all bytes from the input stream to the output stream, as you already did in your code example.
Use myClass.getClassLoader().getResourceAsStream("loadAtRuntime.dll"); and you will be able to find and copy DLLs in the JAR. You should pick a class which will also be in the same JAR.

Creation gzip archive using Apache Commons Compress

I succeed to create gz archive with expected content, but how can I set the filename inside the archive?
I mean, if archive myfile.gz was created, the file inside it will be named "myfile", but I want to name it like source file, for example, "1.txt"
Current code:
public static void gz() throws FileNotFoundException, IOException {
GZIPOutputStream out = null;
String filePaths[] = {"C:/Temp/1.txt","C:/Temp/2.txt"};
try {
out = new GZIPOutputStream(
new BufferedOutputStream(new FileOutputStream("C:/Temp/myfile.gz")));
RandomAccessFile f = new RandomAccessFile(filePaths[0], "r");
byte[] b = new byte[(int)f.length()];
f.read(b);
out.write(b, 0, b.length);
out.finish();
out.close();
} finally {
if(out != null) out.close();
}
}
GZip compresses a stream. Typically, when people use GZip with multiple files, they also use tar to munch them together.
gzip archive with multiple files inside

Copying / Backingup database to SD card Android

I am using this code and it keeps only getting to the output file line and throws the exception then. Can anyone see what the issue might be with this line?
try{
Log.e("Trying","try");
// Local database
InputStream input = new FileInputStream("/data/data/package/databases/database");
Log.e("Input","in");
// create directory for backup
// Path to the external backup
OutputStream output = new FileOutputStream("/sdcard/android/package/databases/mydatabase.db");
Log.e("Output","out");
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
Log.e("Buffer","Buff");
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
Log.e("After While","try");
output.flush();
output.close();
input.close();
} catch (IOException e) {
throw new Error("Copying Failed");
}
I doubt this is the cause of your problem, but the following two lines:
OutputStream output = new FileOutputStream("/sdcard/android/package/databases/mydatabase.db");
InputStream input = new FileInputStream("/data/data/package/databases/database");
should be turned into:
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard, "android/package/databases/mydatabase.
File data = Environment.getDataDirectory();
File inputFile = new File(data, "data/package/databases/database");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
This is because the sdcard and data directories may be in different places on different phones. And often, you need to do /mnt/sdcard/ to actually reference the sd card, but the best way is still to use Files and use Environment as I showed above.
You would of course need to put it all into a try block as you already have, and then within the catch, you will need to put e.printStackTrace(); and then if an error is thrown, you can look at the errors from the logcat to determine where your code fails instead of using a log every couple of lines.
And, in your Manifest you have to have the following permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories

Resources