I wrote this code to read the content of a file to a bytes array.
It works fine when path (given in the constructor) is relative. But I would like it to work in an absolute path instead. I looked up in java File class docs but got confused. How can I changed it to work with absolute path?
File file = new File(path);
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray);
fis.close();
In your code here;
File file = new File(path);
Your path String variable just needs to be absolute instead of relative.
I don't see why it would not work. Did you try to update the path variable to absolute path of your file?
I think you can create an object from the Path interface using a relative path and getting that path using the Paths class using the static get method. once done you can get the absolute path from the created object and use it as a string if you prefer.
I hope I have helped you.
Path path1 = Paths.get("res/ficheroPrueba.txt");
File file = new File(path1.toAbsolutePath().toString());
Related
I am giving image path as ://serverIp/image/xyz.jpg but I am not showing image on my client application.
For this what i have to do?
I am writting my code as: FileInputStream fin = new FileInputStream(path);
I assume you're using Windows network shares? If so, you should use UNC paths. Your path isn't a valid UNC path, so your file will not be found. Also, for reading files from shares, you could use an URI.
Try the following:
File filetoRead = new File(new URI("\\\\serverIp\\image\\xyz.jpg"));
FileInputStream fin = new FileInputStream(fileToRead);
Note that all backslashes are used twice, this is done to escape the backslashes.
This code works only for the class in the same package. I need to load the file from absolute path like c:/home/lpl/asm/hello.class
Any one please help me do this
InputStream in=ASMHelloWorld.class.getResourceAsStream("/aasm/ClassModificationDemo.class");
ClassReader classReader=new ClassReader(in);
To load a file from an absolute path:
String path = "c:/home/lpl/asm/hello.class";
InputStream in = new FileInputStream(path);
ClassReader classReader = new ClassReader(in);
Obviously hardcoding the path like this, would seriously restrict portability, so the path should be obtained from a command line parameter, user input, a properties file etc.
Why would you want this? If you do something like that you disable the ability to use the program on a different computer, unless you recreate that folder structure.
Seems like a duplicate question to me, just with a different file: Use Absolute path for ClassLoader getResourceAsStream()
Is there a easy way to get the filePath provided I know the Filename?
You can use the Path api:
Path p = Paths.get(yourFileNameUri);
Path folder = p.getParent();
Look at the methods in the java.io.File class:
File file = new File("yourfileName");
String path = file.getAbsolutePath();
I'm not sure I understand you completely, but if you wish to get the absolute file path provided that you know the relative file name, you can always do this:
System.out.println("File path: " + new File("Your file name").getAbsolutePath());
The File class has several more methods you might find useful.
Correct solution with "File" class to get the directory - the "path" of the file:
String path = new File("C:\\Temp\\your directory\\yourfile.txt").getParent();
which will return:
path = "C:\\Temp\\your directory"
You may use:
FileSystems.getDefault().getPath(new String()).toAbsolutePath();
or
FileSystems.getDefault().getPath(new String("./")).toAbsolutePath().getParent()
This will give you the root folder path without using the name of the file. You can then drill down to where you want to go.
Example: /src/main/java...
I think I am really close, but I am unable to open a file I have called LocalNews.txt. Error says can't find file specified.
String y = "LocalNews.txt";
FileInputStream fstream = new FileInputStream(y);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Name of file is LocalNews.txt in library called News....anyone know why the file will not open?
The file is in the same Java Project that I am working on.
Error: LocalNews.txt (The system cannot find the file specified)
Project is named Bst, package is src in subPackage newsFinder, and library that the text files are stored in is called News.
Found out it was looking in
C:\EclipseIndigoWorkspace1\Bst\bin\LocalNews.txt
But I want it to look in (I believe)
C:\EclipseIndigoWorkspace1\Bst\News\LocalNews.txt
But if I make the above url a string, I get an error.
String y = "LocalNews.txt";
instead use
String y = "path from root/LocalNews.txt"; //I mean the complete path of the file
Your program can probably not find the file because it is looking in another folder.
Try using a absolute path like
String y = "c:\\temp\\LocalNews.txt";
By 'library called News' I assume you mean a jar file like News.jar which is on the classpath and contains the LocalNews.txt file you need. If this is the case, then you can get an InputStream for it by calling:
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("LocalNews.txt");
Use
System.out.println(System.getProperty("user.dir") );
to find out what your current directory is. Then you'll know for sure whether your file is in the current directory or not. If it is not, then you have to specify the path so that it looks in the right directory.
Also, try this -
File file = new File (y);
System.out.println(file.getCanonicalPath());
This will tell you the exact path of your file on the system, provided your file is in the current directory. If it does not, then you know your file is not in the current directory.
I'm having problems setting the path of the zip file, X, in ZipFile zipfile = new ZipFile("X");.
I don't want to hardcode the path such that it becomes ZipFile zipfile = new ZipFile("C:/docs/data.zip");.
I want to do something like :
ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());
Where the path of the zip file is determined by the selection of the user. But, this gives an error, because this only works for InputStream.
Previously, I've already retrieved the multipart/form data and gotten the real path of the zip file:
String path = getServletContext().getRealPath("/WEB-INF");
UploadBean bean = new UploadBean();
bean.setFolderstore(path);
MultipartFormDataRequest multiPartRequest = new MultipartFormDataRequest(request);
bean.store(multiPartRequest); //store in WEB-INF
// get real path / name of zip file which is store in the WEB-INF
Hashtable files = multiPartRequest.getFiles();
UploadFile upFile = (UploadFile) files.get("file");
if (upFile != null) request.setAttribute("myFile", upFile.getFileName());
Any solutions to this?
You can convert webcontent-relative paths to absolute disk file system paths in two ways:
Just use ServletContext#getRealPath() as you previously already did.
ZipFile zipfile = new ZipFile(getServletContext().getRealPath("/WEB-INF/" + request.getAttribute("myFile").toString()));
Use ServletContext#getResource() instead. It returns an URL. Call getPath() on it.
ZipFile zipfile = new ZipFile(getServletContext().getResource("/WEB-INF/" + request.getAttribute("myFile").toString()).getPath());
Way #1 is preferred.
I don't understand why you don't use the real path that you already have.
Anyway, you can work with a ZipInputStream.
This way you can handle your file as a simple Stream. The only big differences are the getName() method and size() that you can't directly access. With a ZIS you will be able to read every entry.
Resources :
Javadoc - ZipInputStream