I have a text file which I downloaded from the internet. File is large, somewhat around 77MB and I need to map it into the memory so I can read it fast. Here is my code
public class MapRead {
public MapRead()
{
try {
File file = new File("E:/Amazon HashFile/Hash.txt");
FileChannel c = new RandomAccessFile(file,"r").getChannel();
MappedByteBuffer buffer = c.map(FileChannel.MapMode.READ_ONLY, 0,c.size()).load();
System.out.println(buffer.isLoaded());
System.out.println(buffer.capacity());
} catch (IOException ex) {
Logger.getLogger(MapRead.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
No good, this generated the below output.
false
81022554
This is my first time trying this. I have no idea what went wrong, or what to do next, to read the file.
Related
The below code works when running from my editor but the image fails to load when compiled into a runnable jar file with eclipse.
public static BufferedImage getRandomImage() {
// returns a random image from the Images folder
Random rand = new Random();
URL res = Card.class.getResource("Images"); // located in /src/.../Images
File f = new File(res.getFile());
if (!f.exists()) {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
}
File[] files = f.listFiles();
int random = rand.nextInt(files.length);
BufferedImage img = null;
try {
img = ImageIO.read(files[random]);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
Could someone please suggest how I can modify my code or editor to load the files when compiled.
I have read other methods of accessing files but since I need to select randomly from a folder, I need to use the File class.
There is no safe way to list resources at runtime.
(Some people may suggest approaches which work sometimes, but will not work all the time. Class.getResource is not guaranteed to provide a listing; ProtectionDomain.getCodeSource can return null.)
But you don’t need to. It’s your application; you already know what files you put into it.
The best way is to either hard-code the list of files, or include a simple text file that contains a list of the files.
As an example, assume you created (or generated) a file named image-files.txt in which each line contains the base name of an image file, and embedded that file in your application:
List<String> imageNames;
try (BufferedReader linesReader = new BufferedReader(
new InputStreamReader(
Card.class.getResourceAsStream("image-files.txt"),
StandardCharsets.UTF_8));
Stream<String> lines = linesReader.lines()) {
imageNames = lines.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
int random = rand.nextInt(imageNames.length());
String imageName = imageNames.get(random)));
BufferedImage img;
try {
img = ImageIO.read(Card.class.getResource(imageName));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return img;
Note: The getFile() method of URL does not return a valid filename. It only returns the path portion of a URL. There are many characters which would be illegal in URLs, so the path portion percent-escapes them. If you ignore this fact, the value returned by getFile() will eventually fail.
(The reason for the misleading method name is that the URL class was part of Java 1.0, and in the mid-1990s, all URLs actually referred to physical files.)
I need to use the File class
Each .jar entry is just a subsequence of compressed bytes within a single .jar file, so you will never be able to use File to read such an entry. Class.getResource and Class.getResourceAsStream are the only correct ways to read those entries.
The problem is that you are trying to access a URL of a resource as a file.
with this you can get all the images, and then you can do this:
List<String> arr = getResourceFiles("Images");
String imgPath = arr.get(rand.nextInt(arr.size()));
InputStream stream = Card.class.getResourceAsStream("Images/" + imgPath);
try {
img = ImageIO.read(stream);
} catch (IOException e) {
e.printStackTrace();
}
return img;
I have a rest service in Java EE and for some weird backward compatibility reasons I have to return an .mdb file from a URL request after adding some rows inside it.
At first I simply opened an mdb file, cleared all rows in it, wrote my rows and returned it to the caller, however I realized the .mdb kept growing this way because Access doesn't purge the rows on deletion but only erases it and the library I am using (Jackcess) doesn't support purging completely the rows.
So I switched to creating a copy of an empty .mdb file with java.io.File.createTempFile() and returning it however a dangling pointer to the file in the /tmp/ folder is left and after several days I get a
java.io.FileNotFoundException: /tmp/tmpdb.mdb04949499 (Too many open files)
The only solutions I found so far are:
Set MAX_FILE_HANDLES_FOR_READ_ENDS_MAP to a very high number (which only postpones the problem)
deleting the temp file, which however is not viable because I return it from a function and once returned I lose control of the pointer.
below what I currently have:
GET
#Path("/get/database/{filename}")
#Produces("application/jet")
public StreamingOutput getDatabase(#PathParam("filename") String fileName)
{
//access files increase indefinitely in size because deleted rows are simply marked "deleted" and not removed
//so we create a temporary file equal to the template .mdb file and use it instead
java.io.File myDBFile = null;
try
{
java.io.File templateDBFile = new java.io.File(url + "resources/clean_tmpdb.mdb");
myDBFile = java.io.File.createTempFile("tmpdb", ".mdb");
myDBFile.deleteOnExit(); //useless hint
FileChannel src = new FileInputStream(templateDBFile).getChannel();
FileChannel dest = new FileOutputStream(myDBFile).getChannel();
dest.transferFrom(src, 0, src.size());
}
catch (IOException ex)
{
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
if (src != null)
{
try
{
src.close();
}
catch (IOException ex)
{
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (dest != null)
{
try
{
dest.close();
}
catch (IOException ex)
{
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* work on the file inserting rows */
return new FileStreamingOutput(myDBFile);
}
EDIT: found a similar question, with a vague accepted answer: How to delete file after REST response, the accepted answer is "just write directly to the output stream contained in the Response."
You aren't closing either src or dest. So as to ensure they are closed, close them in finally blocks, or use the try-with-resources syntax.
return new FileStreamingOutput(myDBFile);
/* here I should call myDBFile.close(); and myDBFile.delete(); */
Here you can't call myDBFile.close(), as there is no such method. You can't call myDBFile.delete() either, as otherwise the caller will receive a File that doesn't exist. If the File needs to be deleted, the caller will have to do it. Your stated requirement doesn't make sense.
Sorry if I sound unexperienced or make no sense, I'm currently in my second computer science year and I'm currently making a light 2D game engine in java for a school project ( We can choose what we want to do for this one ).
So I'm currently working on the asset manager. For now it contains a hashmap with assets and a InputStreamProvider.
The InputStreamProvider is an interface between the assetManager and different kind of providers. For now, I only have a FileProvider which provides a stream from a file.
For now everything is ok, but when it comes to actually retrive the file from an id, I got a small problem with the extension. I thought that one of the advantages of using an asset manager was that you could just use IDs for your assets, which is fine once they are loaded, but when you need to load them how do you know the file extention?
Here is my workaround which is obviously not a good solution using a config file which lists the extentions being used for the assets and then trying to load the file with every extention.
public class FileProvider extends AssetInputStreamProvider {
private final static String assetLocation = "assets/";
protected final static List<String> assetExtentions = new ArrayList<>();
static {
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("conf/assets.cfg")));
while (isr.ready()) {
String ext = new String();
Character c = (char) isr.read();
while (!c.equals(',')) {
ext += c;
c = (char) isr.read();
}
assetExtentions.add(ext);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(AssetInputStreamProvider.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AssetInputStreamProvider.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public InputStream getInputStream(String type, String name) {
File file;
for (String assetExtention : assetExtentions) {
try {
System.out.println(assetLocation + type + "/" + name + "." + assetExtention);
file = new File(assetLocation + type + "/" + name + "." + assetExtention);
return new FileInputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileProvider.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
}
So I'd like to know if there is a way to load a file without knowing its extention, or if I'm completely mistaken about the way to load assets and then if anyone could tell me the way to do it properly it would be really helpful.
And out of curiosity, it's out of the scope of my question but I've seen some people using integer for IDs but no matter how much I think about it, I can't figure how to do it without hardcoding resources and giving them an int value so I'd like to get some hints to know how to implement such a system too.
Thanks a lot for reading and have a good day.
Is it possible to simply dump an ArrayList to an XML file?
I have an array-list which contains various types of message objects.
What I would like to do is save this to an XML file to keep a log.
What I have tried is this.
public void saveConversation()
{
FileOutputStream f_out;
try {
f_out = new FileOutputStream("convo.txt");
// Write object with ObjectOutputStream
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
// Write object out to disk
obj_out.writeObject (convo);
} catch (IOException ex) {
Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
}
}
This works but the file created doesn't make much seance. I was wondering if there was a simple way to change this to readable XML?
Take a look at XStream, it is designed for exactly this task.
I'm currently building an android application that displays a set of pdf files in a ListView. Instead of just displaying the filename I want to grab the Title from the metadata of the pdf and display that in the list, if the file doesnt have a Title set then just use the filename. I'm using iText atm, here is what I have:
File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
try {
File f = filteredFiles[i];
PdfReader reader = new PdfReader(f.getAbsolutePath());
String title = reader.getInfo().get("Title");
reader.close();
//Do other stuff here...
} catch (Exception e) {
e.printStackTrace();
}
}
This works fine, its gets the data I want, but its slowww. Also, sometimes I get memory crashes if the file is over 2MB. Is there a better way of doing this? Maybe a way of getting the metadata without having to actually open the pdf file?
Any help is much appreciated, Thanks.
You can try fast PDFParse library. It optimized for performance & small memory consumption.
File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
try {
File f = filteredFiles[i];
PDFDocument reader = new PDFDocument(f.getAbsolutePath());
String title = reader.getDocumentInfo().getTitle();
reader.close();
//Do other stuff here...
} catch (Exception e) {
e.printStackTrace();
}
}