I need to check if any of the 4 uploaded files are same, this check might be on the JSP or Java-Servlet side.
I've been using
var FileName1 = document.getElementById('fileChooser1').value;
var FileName2 = document.getElementById('fileChooser2').value;
if(FileName1 == FileName2)
{
alert("same files cannot be uploaded");
}
But, the problem is that this only deals with the name of the file and this fails if files with same content but different names are uploaded.
So, on apache commons search I found that there is a Default Comparator but I have no idea of how I can use this or is there any other better/simpler way to check for same files.
How can I use the Default Comparator and on what basis does it compare?
Is there any better/simpler solution to this problem in java or javascript?
You can use FileUtils.contentEquals method to compare content of 2 files.
Example
System.out.println(FileUtils.contentEquals(file1, file2));
Related
I have a long JSON file and i want to copy a specific element from it(i know its name) to an excel file.
eg :: Suppose i want to make an excel file having "Product" (Baleno, i20, Ford Figo etc) imported from a JSON file, how to do it using GET POST or without AJAX.
So, obviously there are ways to write this yourself. What I recommend, however, is using a library (or two. I'd recommend JSON Simple and/or Apache POI) Software engineering is about efficiency, and that includes for the engineer. Using libraries is not shameful. I'd recommend doing that first. Try out using librarys, okay?
-Batista
One simple method I have used, when you only require the content you have in the JSON and if the output needs no formatting!
Create/Construct/Return a CSV File containing the content.
Product,Q1Sales,Q2Sales,Q3SalesQ4Sales
"Baleno",6000,5000,7000,5500
Return the Mimetype Filename as "BalenoSales.xls"
Make the Suffix of the Servlet URL ".xls" as well so Excel/IE likes it.
Dear brothers Hope you all right?
I'm designing a document program, however, rather to save file .text extension or using any other MS-Office API in java, i want to create my custom file format such as ".sad" extension so that this sort of file can only be read by my programs, how this can be possible?
Your requirement seems ambiguous. Are you looking to make a program that creates MS Office Word documents or plain text files with a custom file extension?
In the case of the former, you can't have a custom extension as MS Word documents, by definition, have a .doc / .docx extension.
However, if you are looking to create a program that produces text files then you can easily have a custom extension. Just look at this tutorial: How to create a file in Java
I already stated why this is a bad idea. Yet I have a solution for you (more like a how-not-to-do-it)
Take your plain text you want to save, convert it to bytes and apply this "highly enthusiastic encryption nobody will ever be able to break" on it:
string plainText = "yadayada";
bytes[] bytesFromText = toBytes(plainText);
bytes[] encrypted = new Array(sizeof(bytesFromText)*2);
for(int i = 0; i < sizeof(bytesFromText); i++){
if((i modulo 2) == 0){
encrypted.push(toByte(Math.random modulo 255));
}
encrypted.push(bytesFromText[i]);
}
I let it up to you to figure out why this is a bad idea and how to decrypt it. ;)
You can create file with any extension
For example,
File f = new File("confidential.sad");
Hope this will work for you :)
Working with custom files in Java
Here is the tutorial that will help you in getting the concept about how to create your own files with custom extension such as .doc or .sad with some information embedded in it and after saving the file you want to read that information form the file.
ZIP
Similar applications often use archives to store data. Consider MS-Word and its documents >with the .docx file extension. If you change the extension of any .docx file to .zip, you >will find that the document is actually a zip archive, with only a different extension.
https://www.ict.social/java/files/working-with-custom-files-in-java-zip-archive
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(file,"file.custom");
I have got a directory listing as a String and I want to retrieve a particular part of the string, the only thing is that as this is a directory it can change in length
I want to retrieve the file name from the string
"C:\projects\Compiler\Compiler\src\JUnit\ExampleTest.java"
"C:\projects\ExampleTest.java"
So in these two cases I want to retrieve just ExampleTest (the filename can also change so i need something like get the text before the first . and after the last \). Is there a way to do this using something like regex or something similar?
Why not use Apache Commons FileNameUtils rather than coding your own regular expressions ? From the doc:
This class defines six components within a filename (example
C:\dev\project\file.txt):
the prefix - C:\
the path - dev\project\
the full path - C:\dev\project\
the name - file.txt
the base name - file
the extension - txt
You're a lot better off using this. It's geared directly towards filenames, dirs etc. and given that it's a commonly used, well-defined component, it'll have been tested extensively and edge cases ironed out etc.
new File(thePath).getName()
or
int pos = thePath.lastIndexOf("\\");
return pos >= 0? thePath.substring(pos+1): thePath;
File file = new File("C:\\projects\\ExampleTest.java");
System.out.println(file.getAbsoluteFile().getName());
Java code
String test = "C:\\projects\\Compiler\\Compiler\\src\\JUnit\\ExampleTest.java";
String arr[] = test.split("\\Q"+"\\");
System.out.println(arr[arr.length-1].split("\\.")[0]);
This is the regex in c# and it works in java :P too.Thanks to Perl.It matches in Group[1]
^.*\\(.*?)\..*?$
How would I go about converting an abstract File path (of File type) into a String type?
File.getPath() will give you the path as a String.
If you want the contents of the file, use either of
IOUtils.toString(InputStream,Charset) from Apache Commons-IO
Files.toString(File,Charset) from Google Guava.
Use File.getAbsolutePath().
check this link Abstarct path to String as path
check the method getPath()
The simplest way to get file path as a string is as below code:
String FilepathAsString = fileobject.getAbsolutePath();
Import required : java.io.File;
If you want to load the contents of the file into a String, using google guava it's a case of
String st = Files.toString(file, Charsets.UTF-8);
see the Javadoc.
Of course this does require you to know the character set of the file. Files contain bytes, strings are characters, and there always end up being bugs when converting between the two if you don't actually know what the bytes in your file mean.
i have different format files in DB. i want to copy to my local machine.
how can i identify the file format (doc, xls, etc...)
Regards,
krishna
Thanks, for providing suggestions... based on your suggestions i had written the code & i am completed...
please look into my blog.. i posted the code over here...
http://muralie39.wordpress.com/java-program-to-copy-files-from-oracle-to-localhost/
Thank you guys..
Thanks,
krishna
If your files are named according to convention, you can just parse the filename:
String filename = "yourFileName";
int dotPosition = filename.lastIndexOf(".");
String extension = "";
if (dotPosition != -1) {
extension = filename.substring(dotPosition);
}
System.out.println("The file is of type: " + extension);
That's the simplest approach, assuming your files are named using some kind of standard naming convention. The extensions could be proprietary to your system, even, as long as they follow a convention this will work.
If you need to actually scan the file to get the format information, you will need to do some more investigation into document formats.
How are the files stored? Do you have filenames with extensions, or just the binary data?
Mime Util has tools to detect format both from extensions and from magic headers, but of course that's never 100%.
You can use the Tika apache library.
As Dmitri pointed out however, you may have incorrect results sometimes if detecting mime type from file headers or file extension.