How to take notice of filenames in different locale in Java - java

My application is sifting through a M3U playlist and generating a Windows batch file (copy_files.bat) using the following command:
printWriter = new PrintWriter("copy_files.bat", "UTF-8");
It generates the appropriate batch file that mostly works, but fails to copy some files that have foreign characters in their filenames. The same behavior occurs when using Java's built in function for copying files (few files cannot be found on the system due to filename character encoding). Please advise!

To wrap it up, I cannot import some playlists that are M3U, but M3U8 playlist contains the data that can be used. Here's a small project to test this.
http://m3uexporttool.sourceforge.net/

Related

How can an app walk through the contents of one of its packages at run time? [duplicate]

This question already has answers here:
How to list the files inside a JAR file?
(17 answers)
Closed 1 year ago.
This method does not apply:
Files.walk(Paths.get("folder name"))
Because when the app is running, packaged as a jar, it does not contain File objects.
Is there a method for an app to walk through all the contents of one of its packages while it runs?
There is; open the jar file (using java.io.JarFile) and walk through the entries returned by entries(). There's also JarInputStream if your jar is being streamed in from elsewhere.
But, it sounds like you have equated 'jar file' with 'my application, at runtime'.
That's a tricky move. For starters, figuring out your own jar is possible but a little hacky, and more importantly, it then means your app straight up fails to work unless it is in jar form. This complicates deployment, development, and debugging.
There is no need to do it this way.
You can ask java for resources from 'whereever it is currently picking up class files'. A class file is a resource crucial to the running of your application. So is e.g. an icon file for a GUI app. Both should be coming from the same place, and where that is? That's abstracted away, and you should follow along with the abstraction, so that the 'load resource' code works just as well in debugging as it does at runtime.
The system for this is MyClass.class.getResource("file.txt") which will look for file.txt in the same place MyClass.class is located, even if it is in jar files, generated live, or obtained from a BLOB object in a database someplace, or streamed over a network.
The downside is, this system does not have an abstraction for listing files. Only for getting resources with a specific name.
The SPI system is the solution: Make a file that lists (one resource per line) the resources - instead of 'list dir', you 'parse each line of CONTENTS.txt' for the equivalent operation. You can then use annotation processors, if you must, to automatically create and maintain this content file.
ServiceLoader is baked into the JDK itself, but it's designed to load, specifically, class files and not other resources. But the principle is trivial and can be handwritten in about 5 lines of code.
You can iterate the contents of any ZIP / JAR file using Files / NIO, but you need to access the ZIP filesystem. Then you can call Files.find or Files.walk on the contents of the ZIP / JAR archive.
For example this will print the contents of every Path in a jar:
Path zip = Path.of("your.jar");
BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> true;
try (FileSystem fs = FileSystems.newFileSystem(zip)) {
for (Path root : fs.getRootDirectories()) {
try(Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, predicate)) {
stream.forEach(System.out::println);
}
}
}
Once you have a Path object from the ZIP FileSystem, you can access the contents of the entries using NIO Files calls in the normal manner such as with Files.newInputStream or Files.copy.

How to swap file path and queries from Java code

I have a "how-should-I-implement- it- on -the- best- way -in -java" question. I have written a program in Java that reads a csv file that is stored on a fixed directory. (BufferedReader & FileReader) The file is then read out and the data is displayed in an XYDataset (XY graph) using jFreeChart.
Question: The program should now run without a development interface, that means I export it as .exe. However, since the directory is now to be "independent", that means it can be changed at any time, I should now save the directory externally and / or stored it should be without the code must be changed. In addition, the data from the directory (csv-files) should also be called regardless of the code. That means there are different .csv files inside the directory & you should sort by name & most recent date. That It should always open the latest .csv file with the "1" in the name and another time the .csv file with the "2" in the name. My question is now: How should I implement this best?
My first idea would be an XML file to access my Java program, but I do not know exactly how I should store the file path there.
maybe like this: ? file: /// H: /Test/Testfile.csv
Does somebody know how to "outsourced" & then accessed a directory or other ideas, how I could implement that outside the "real" code?
Would be very happy about help!

Could using Java.IO.File cause data to scramble on an AS400 IFS?

I am building a small java application that will take in XML files and convert them to text files. The end result being a jar that sits on the IFS to convert files when called from a shell script run by a RPGLE program. (Not my idea, just charged with making it happen)
To do so I am using JAXB to unmarshall these XML files into JAXB annotated POJOS before putting them into a text file using the a new IFSFile and IFSFileOutputStream. This works great with the exception that the order output data is scrambled or out of its proper order.
I created a second version of the Jar that replaced the IBM Toolbox classes for standard Java.IO classes. This version of the jar when run from Windows outputs the results in the proper order. This same Jar (Java.IO version) run from the 400 itself returns scrambled data too.
Both Jars however use a standard Java.IO file for the XML inputs. This is because JAXB will not accept an IFSFile as an input. See the below code:
File inputFile = new File(source);
JAXBContext context = JAXBContext.newInstance(PriceRecords.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
PriceRecords priceRecords = (PriceRecords) unmarshaller.unmarshal(inputfile);
List<PriceRecord> data = priceRecords.getPriceRecords();
I read somewhere that the AS400s save/read files differently than a standard Linux or Windows OS would. So I am wondering if someone can shed some light on this. If the scrambled data isn't caused by the above inputFile being the Java.IO.File class used on a AS400, what else could cause this difference in functionality?

How to make File Reading Platform Independent in Java

Hi I am using Maven for Selenium automation tests using Java. I am using Excel Sheet to read data for filling up a registration form. Now It is a normal Maven archetype.
src
--main
--java
--mypackage for coding goes here.
--resources
--data
-- the excel sheet
--test
-- some stuff here under java
currently if I want to read a file I am writing src/main/resources/data/theexcelsheet.xlsx
but when I share this jar, I think this will break and I don't want this to break if I package this jar.
How do I make it platform or format independent.
I am using APACHE POI Api if you are thinking how I am reading files.
Someone reading gave me idea that I might be able to use MANIFEST files to do this but I am not sure, can someone help?
If you use getResource() to point to your file, the syntax of the path always uses / to separate the components, never \ so you do not have to worry.
In general, outside of a jar, for example if you want to save application settings in a folder, you have to use a OS specific separator - you can obtain it this way:
System.out.println("my" + File.separator + "dir");
Returns my\dir on Win and my/dir on Linux
Of course, that's not enough as you cannot use hardcoded paths like the "c:" drive, but you can get the most common paths reading the System class properties, eg:
System.out.println(System.getProperty("user.home"));
returns C:\Users\piero in Windows7 and /home/piero in Linux

Java Desktop Application - how to obtain list of files with similar file names in a specific folder

I have a Java Desktop Application, in which at an intermediate stage, some files with the following file names are generated
file-01-1.xml
file-01-2.xml
file-01-3.xml
and so on.
The number of files with such names is variable, I want to determine the total number of files of above type of name, so that I can then do further processing on these files. (THese files are generated by a DOS command which does not give number of files generated in its output/number of files generated varies depending on input file, hence this problem).
You can implement pure java solution using File.list(), File.listFiles() combining them with FileFilter. Both methods return arrays, so you can retrieve the array length to get number of files.
This method might be ineffective if number of files is very big (e.g. thousands). In this case I'd suggest you to implement platform specific solution by executing external command like sh ls file* | wc -l.
You can use a custom FilenameFilter when listing file in the output folder.
See: http://download.oracle.com/javase/6/docs/api/java/io/File.html#list%28java.io.FilenameFilter%29
Use File.listFiles(FilenameFilter) or similar methods.

Categories

Resources