read files names from directory without System files(.DS_Store) - java

Im trying to read all filenames from a folder and save them in an array but
im still reading all files (including the system folder .DS_Storefrom)so that my code doesn´t work for what i want to do. anyone knows how to read only non Systems files/folder?
Thanks for the help!
Here´s what i wrote,
String pathLevel= "/Users/lan/Desktop/top/";
File file = new File(pathLevel);
String [] levelNames = file.list();
String [] matrix= new String[levelNames.length];

Related

Copy data from one csv file to another csv file using java

I'm working on a Java code to copy the data from one csv file into another csv file .
The requirement is that files (multiple files) that are uploaded in a particular path has to be copied one at a time into another csv file(say tag.csv) in a different location.Later tag.csv will be picked up by a shell script and connect to Oracle DB to run a stored procedure .All of this is done repeatedly until all the uploaded files is processed and shell script is triggered for each file separately.
Now I'm in stuck in copying the csv data .
I have tried using buffered reader,filewriter etc. but i'm unable to copy the data to tag.csv,but I could just read them .
Since Im new to java im finding it hard to understand where im going wrong.
Help is much appreciated.
You can simply use the Java 7 NIO2:Eg:
If you want to copy a file from one location to another, simply call:
Files.copy(fromPath, toPath);
If you want to move:
Files.move(fromPath, toPath);
With Java 7 features, you don't need to write hard code for files handling. Hope it help.
Java 7 NIO2 Tutorial Link
Edited:
But your requirement is not file copy but you want to write uploaded file contents to existing file, you can also simply use the Java 7 NIO2 features.
Eg:
private static void writeFileAsAppend() throws IOException {
List<String> lines = readFileAsSequencesOfLines();
Path path = getWriteFilePath();
Files.write(path, lines, StandardOpenOption.APPEND);
}
private static List<String> readFileAsSequencesOfLines() throws IOException {
Path path = getReadFilePath();
List<String> lines = Files.readAllLines(path);
return lines;
}
private static Path getReadFilePath() {
Path path = Paths
.get(".\\ReadMe.csv");
return path.normalize();
}
private static Path getWriteFilePath() {
Path path = Paths
.get(".\\WriteMe.csv");
return path;
}

open and read random .txt with java aplication executable not knowing the rute of .txt

I would like to know how I can open and display a .txt in a Java application . The .txt is associated with the application and when you click on it , the application opens, but the file does not get to be shown if not by passing a fixed route.
I've got to show it but only if the .txt file is in the same directory as the jar file and run the application only if directly . The direct access from the .txt opens the application but nothing more .
I have this code , you see the path step them directly . I want you to take from the .txt has been clicked .
FileReader f = new FileReader("archivo.txt");
BufferedReader b = new BufferedReader(f);
String linea_cliente = b.readLine();
StringTokenizer datos_cliente = new StringTokenizer(linea_cliente,";");
while(datos_cliente.hasMoreTokens()){
pedido.setText(datos_cliente.nextToken());
id_cliente.setText(datos_cliente.nextToken());
nom_cli.setText(datos_cliente.nextToken());
dir_cli.setText(datos_cliente.nextToken());
cp_cli.setText(datos_cliente.nextToken());
loc_cli.setText(datos_cliente.nextToken());
prov_cli.setText(datos_cliente.nextToken());
pais_cli.setText(datos_cliente.nextToken());
obs_cli.setText(datos_cliente.nextToken());
}
Sorry for my bad English . Thank You ;)
FileReader f = new FileReader("archivo.txt");
Implies that archivo.txt is a relative path. Relative meaning in relation to the current executable. It is an implied .\archivo.txt
You can place it in a sub directory and use a relative path again like .\myfiles\textfiles\archivo.txt where .\ is the location of your jar.
If you want to input many different text files and you don't know where they will be then you can use arguments. From the command line it would look like:
> java jar myproj.jar C:\test\foo\archivo.txt
And to access it in main() use:
String filePath = args[0]
FileReader f = new FileReader(filePath);
If you want it to be portable accross many systems you'll need to take advantage of environment variables to get your base path and then attach the route to your .txt file to the base.
Sorry, it was a little unclear what you were asking for so I covered a few common cases, let me know if you need clarification.

Getting txt from jar & stucked

I currently started programming a board game playground which can load different games. I store these games in file named config.txt, but I am having trouble accessing it. Firstly, I started with my favourite file approach:
String fileAddress = "./resources/config.txt";
fis = new FileInputStream(fileAddress);
reader = new BufferedReader(new InputStreamReader(fis));
But then when I built .jar file, it stopped working. For obvious reasons. So I found myself looking around and I found about recommendation to use getResourceAsStream(String s) method. So I changed my code to following way:
String fileAddress = "./resources/config.txt";
InputStream toReturn=null;
toReturn = this.getClass().getClassLoader().getResourceAsStream(fileAddress);
But here I got stuck. No matter how I tweak the fileAddress (tried ./config.txt ./resources/config.txt ./resources/boardgames/config.txt and ./resources/boardgames/config.txt) and both using or omitting getClassLoader(), the result of toReturn just always equals NULL and ends up throwing NullPointerException very soon.
Location of required file follows. As I do not have enough reputation, I will have to ASCII art the file hierarchy. Both config.txt are valid targets.
On File System:
Boardgames
src
cache
classes
resources
boardgames
config.txt
imgs
config.txt
Inside Jar File
BoardGames.jar
boardgames
<class files>
config.txt
imgs
META-INF
config.txt
Therefore I would need an assistance with repairing the code so that it would read the file properly. If you could include tips how to get ImageIcon from the .png files located in subfolders imgs, since I reckon I will run into similar problem once I get past the initialization phase via config.txt.
Thank you for all your help.
thanks to #EJP and #immibis for all help.
this.getClass().getResourceAsStream("boardgames/config.txt");
was the solution that finally got me running

Java reading file txt from URL

I would read txt content from web, I know how to read a local file but I want to put my txt file in a website and read it in my application. I used this code :
BufferedReader reader = new BufferedReader(new InputStreamReader((new URL("MY TXT URL")).openStream()));
String line = reader.readLine();
int k =0;
String[] SaveLine = new String[20];
for( k = 0; line!=null; k++)
{
System.out.println(reader.readLine());
line=reader.readLine();
}
There is a problem, with this code I read the entire webpage, so the HTML is included. How can I just read the file content ? There is a specified website where I could put my txt files for read them?
Thanks in advance!
If this "MY TXT URL" is indeed your TXT file's URL
(for example http://www.test.com/file10.txt), then your
code will read the TXT file itself, and not any web page (as you say it does).
So you're mixing something here.
Whatever URL you give to it, that web resource (file) it is going to read.
This is a very confusing problem. Try this. Make the URL include the location and name of the textfile; here is an example: www.yourwebsiteurl.com/yourtextfile.txt. Using that method, ONLY the textfile, not including HTML, will be read.
If the endpoint of your URL is a text file, if shouldn't be displaying any html (unless that's what's contained in your text file.)
allegedly uploadedit works though I haven't used it myself.
Thanks to all, I tested my code in a txt uploaded on DropBox and Dropbox probably has code system that I don't know, infact my code returns html/script and not the content of txt. So probably the correct ask is if anybody know a website where can I put my txt file just to reading it? (the url must don't change for ever)
EDIT: PROBLEM SOLVED
I put my file in a website to just read the file and don't read it in a html box (such as a lot of cloud website)

Java filename searching

I need to make a database for book storing. So what I want to do Is basically make a file for each book which I have done.
Now I need to search books when I need them. In 2 functions I'll need to find the book and edit its information.
I have no idea how to get the name of a file and assign it to a scanner\bufferwriter\ect.
Can anyone help me?
You have two options:
Put the names of the files into another, known filename (ie: BookList.txt) and then when your program opens you can parse through this file and get the other file names in the directory.
You can get the file directory listing and store it into a list. For example:
File f = new File("C:\\");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
Once you know the file names, you can open these files and make any necessary edits. Don't forget to also close your file readers and writers once you're done with them!
You can iterate through the array using a for loop. For example:
for(String i : names){ // iterate through each file name in the array
Scanner fileScanner = new Scanner(i);
... // do some parsing
fileScanner.close();
}

Categories