read file system structure from java
By : Mark
Date : March 29 2020, 07:55 AM
will help you Is it possible to read all the names of folders (not sub-folders) inside a directory and save the list in an ArrayList, etc ? , Try this: code :
File dir = new File("directoryName");
File temp;
String[] children = dir.list();
if (children !=null) {
for (int i=0; i<children.length; i++) {
temp = new File(children[i]);
if(temp.isDirectory()) //add children[i] to arrayList
}
}
|
read folder structure from a text file and create the structure - java
By : Ashwin Chandra
Date : March 29 2020, 07:55 AM
it fixes the issue I think this is quite a specific task, and so I don't know about any libraries that would do it for you out of the box. The main idea is that you want to keep note of the current ancestors of the processed line. You can do it with a stack. I wanted to find out if my algorithm actually works so I coded it up and it does work :) Hope it helps. code :
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class DirStuctureReader {
private static final int INDENTATION = 4;
public static void main(String[] args) throws Exception {
Directory result = new DirStuctureReader().read(new File("testfile.txt"));
System.out.println(result);
}
public Directory read(File file) throws IOException {
Scanner scanner = new Scanner(file);
Stack<Directory> directoryStack = new Stack<>();
Directory root = new Directory("/"); // a root directory for everything
directoryStack.add(root);
while (scanner.hasNextLine()) {
processLine(scanner.nextLine(), directoryStack);
}
return root;
}
private void processLine(String line, Stack<Directory> directoryStack) {
int nLeadingSpaces = getNumberOfLeadingSpaces(line);
if (nLeadingSpaces == -1) return;
int depth = nLeadingSpaces / INDENTATION;
while (directoryStack.size() > depth + 1) {
directoryStack.pop(); // discard elements from the stack when we are deep and jump up one or more levels
}
String dirName = line.substring(nLeadingSpaces + 1);
Directory directory = new Directory(dirName);
directoryStack.peek().getChildren().add(directory); // add the directory to the children of the proper parent dir
directoryStack.push(directory);
}
private int getNumberOfLeadingSpaces(String line) {
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) != ' ') return i;
}
return -1;
}
public static class Directory {
private List<Directory> children = new ArrayList<>();
private final String name;
public Directory(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Directory> getChildren() {
return children;
}
}
}
|
How to read and write Java ArrayList Objects to and from a file that has a specific required structure
By : David Wood
Date : March 29 2020, 07:55 AM
Any of those help In your example you use serialization (you can read more about it here Introduction to Java Serialization), which saves an object to a file in binary format. So basically you're saving the whole ArrayList, including its internal fields and values as an array of bytes. But what you really need is simply writing to a text file. code :
PrintWriter p = new PrintWriter("reservations.txt");
p.write("your text goes here");
Passenger p1 = new Passenger(0001, "John Smith", "1A", "AA12");
Passenger p2 = new Passenger(0002, "Annah Smith", "1B", "AA12");
p.write(p1.toString());
p.write(p2.toString());
|
How to read tree structure tab delimeted txt file in Java
By : Adrien Belleflamme
Date : March 29 2020, 07:55 AM
this will help This should work (warning: it could fail with unexpected input, i.e. a line with 2 tabs more than the previous): code :
Scanner keywords = new Scanner(new File("keywords.txt"));
ArrayList<String> stack = new ArrayList<String>();
ArrayList<String> csvLines = new ArrayList<String>();
// stores the number of elements of the last line processed
int lastSize = -1;
while (keywords.hasNext()) {
String line = keywords.nextLine();
int tabs = 0;
// Count tabs of current line
while (line.length() > tabs // to avoid IndexOutOfBoundsException in charAt()
&& line.charAt(tabs) == '\t') {
tabs++;
}
line = line.substring(tabs); // delete the starting tabs
if (tabs <= lastSize) {
// if the current line has the same number of elements than the previous line,
// then we can save the previous processed line as CSV
String csvLine = "";
for (String element : stack) {
if (csvLine.length() > 0) {
csvLine += ", ";
}
csvLine += element;
}
csvLines.add(csvLine);
}
// if the current line has less tabs than the previous, then cut the stack
for (int i = stack.size() - 1; i >= tabs; i--) {
stack.remove(i);
}
// if the current line has more tabs than the previous, then add the new element to the stack
if (tabs >= stack.size()) {
stack.add(line);
}
// save the number of tabs of the current line
lastSize = tabs;
}
keywords.close();
// we have to save the last line processed
if (lastSize >= 0) {
// save line
String csvLine = "";
for (String element : stack) {
if (csvLine.length() > 0) {
csvLine += ", ";
}
csvLine += element;
}
csvLines.add(csvLine);
}
// print out CSV
for (String string : csvLines) {
System.out.println(string);
}
|
Java - Read Hierarchical data from flat structure in text file and build hashmap
By : Ali Rahnavard
Date : March 29 2020, 07:55 AM
should help you out The recursion is already pretty efficient. Here's what you can optimize: Put recursion into a loop Use memoization in recursion/loop (avoid recomputing) Do not recompute ancestors every time getParent is called, precompute results and store them
|