Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Nein, sonst würde ich die List<File> in Strings konvertieren.ganz normal, kannst du es denn mit List<String> usw?
Genau da liegt mein Problem: ich weiss nicht wie ich die Verzeichnisse extrahieren soll, sodass ich einen Knoten daraus erstellen kann.schaue dir Tutorials an, z.B.
How to Use Trees (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
dann baue deinen Baum so auf, wie du es für richtig hälst,
aus den Files kannst du Verzeichnisse zur Zuordnung und Dateinamen zur Anzeige extrahieren,
public Object getChild(Object parent, int index) {
File directory = (File) parent;
String[] directoryMembers = directory.list();
return (new File(directory, directoryMembers[index]));
}
public int getChildCount(Object parent) {
File fileSystemMember = (File) parent;
if (fileSystemMember.isDirectory()) {
String[] directoryMembers = fileSystemMember.list();
return directoryMembers.length;
}
else {
return 0;
}
}
Es soll schon a la Dateiexplorer sein, nur eben kein Abbild der Festplatte sein, sondern aus einer Auswahl an Dateien bestehen, zB alle über 100MB, daher auch die Liste.oder möchtest du dynamisch ein Live-Bild der Festplatte a la Dateiexplorer bauen?
/*
*HINWEIS:
*Die Datei-Objekte sind für Unix formatiert ("file:/"), daher weiss ich nicht ob sich das in Windows ("file:/c:/"??)testen lässt.
*Die Dateien müssen nicht existieren.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.*;
import java.util.List;
import java.util.Map;
public class Nodes {
static List<File> filelist = new ArrayList<File>();
static Map<Integer, File> fileMap;
public static void main(String args[]) {
File f1 = new File("file:/home/subn0de/test");
File f2 = new File("file:/home/subn0de/test01");
File f3 = new File("file:/home/subn0de/Downloads/asdf");
File f4 = new File("file:/home/subn0de/Downloads/skydomes/5313_Filey.png");
File f5 = new File("file:/home/subn0de/1/2/3/winzig");
filelist.add(f1);
filelist.add(f2);
filelist.add(f3);
filelist.add(f4);
filelist.add(f5);
File home = new File("file:/home");
File root = new File("file:/");
System.out.println(root);
System.out.println(filelist);
for(int i=0;i<filelist.size();i++) {
if (filelist.get(i)!=null) {
StringBuilder parentsb = new StringBuilder(), parentsb2 = new StringBuilder();
parentsb.append(filelist.get(i)); //copy file to stringbuilder
if (parentsb.indexOf("/")!=parentsb.lastIndexOf("/")) { //wenn Pfad.length > 0
parentsb2.append(parentsb, parentsb.indexOf("/"), parentsb /**DONE <-- NullPointer in here */ //nächsthöherer knoten
.lastIndexOf("/"));
File parent = new File("file:/" + parentsb2);
String parentContains=parent.toString();
System.out.println("Knoten:\n" + parent + "\nKinder:\n");
for (int i2 = 0; i2 < filelist.size(); i2++) {
/**
* TODO
* - für jedes Blatt im Knoten, wird der Knoten zusätzlich dargestellt. java.util.set anschauen oder Liste zum Abgleich.
* - Knoten ohne Blätter werden nicht angezeigt
*/
if (
filelist.get(i2).toString().contains(parentContains) /**File ist Kind von parentFile*/
&&
filelist.get(i2).toString().lastIndexOf("/")<=parentContains.length()+1 /**Kind != Kind von Kinderknoten*/
&&
filelist.get(i2).toString().length()>parentContains.length()+1 /**Kind != parent*/
){
System.out.println(" +-"+filelist.get(i2));
}//end if filelist.get(i2).toString().contains(parentContains)
}//end for (int i2 = 0; i2 < filelist.size(); i2++) {
}//end if (parentsb.indexOf("/")!=parentsb.lastIndexOf("/")) {
}//end if (filelist.get(i)!=null) {
}//end for(int i=0;i<filelist.size();i++) {
} //end main
} //end class
/** TODO:
* - sortieren
* - mehrfache nodes,so.
*/
file:
[file:/home/subn0de/test, file:/home/subn0de/test01, file:/home/subn0de/Downloads/asdf, file:/home/subn0de/Downloads/skydomes/5313_Filey.png, file:/home/subn0de/1/2/3/winzig]
Knoten:
file:/home/subn0de
Kinder:
+-file:/home/subn0de/test
+-file:/home/subn0de/test01
Knoten:
file:/home/subn0de
Kinder:
+-file:/home/subn0de/test
+-file:/home/subn0de/test01
Knoten:
file:/home/subn0de/Downloads
Kinder:
+-file:/home/subn0de/Downloads/asdf
Knoten:
file:/home/subn0de/Downloads/skydomes
Kinder:
+-file:/home/subn0de/Downloads/skydomes/5313_Filey.png
Knoten:
file:/home/subn0de/1/2/3
Kinder:
+-file:/home/subn0de/1/2/3/winzig
public class Test {
public static void main(String[] args) {
Nodes.main(null);
}
}
class Nodes {
static List<File> filelist = new ArrayList<File>();
static Map<String, List<File>> fileMap = new HashMap<String, List<File>>();
public static void main(String args[])
{
File f1 = new File("file:/home/subn0de/test");
File f2 = new File("file:/home/subn0de/test01");
File f3 = new File("file:/home/subn0de/Downloads/asdf");
File f4 = new File("file:/home/subn0de/Downloads/skydomes/5313_Filey.png");
File f5 = new File("file:/home/subn0de/1/2/3/winzig");
filelist.add(f1);
filelist.add(f2);
filelist.add(f3);
filelist.add(f4);
filelist.add(f5);
File home = new File("file:/home");
File root = new File("file:/");
System.out.println(root);
System.out.println(filelist);
for (int i = 0; i < filelist.size(); i++)
{
StringBuilder parentsb = new StringBuilder(), parentsb2 = new StringBuilder();
parentsb.append(filelist.get(i));
if (parentsb.indexOf("\\") != parentsb.lastIndexOf("\\"))
{
parentsb2.append(parentsb, parentsb.indexOf("\\"), parentsb.lastIndexOf("\\"));
File parent = new File("file:/" + parentsb2);
String parentContains = parent.toString();
System.out.println("untersuche parent: " + parentContains);
if (fileMap.containsKey(parentContains))
{
System.out.println("parent schon bekannt, tschüss");
continue;
}
List<File> childs = new ArrayList<File>();
for (int i2 = 0; i2 < filelist.size(); i2++)
{
File f = filelist.get(i2);
String fst = f.toString();
if (fst.contains(parentContains) && fst.lastIndexOf("\\") <= parentContains.length() + 1
&& fst.length() > parentContains.length() + 1)
{
childs.add(f);
}
}
fileMap.put(parentContains, childs);
System.out.println("saved " + childs.size() + " childs for " + parentContains);
}
}
for (String key : fileMap.keySet())
{
System.out.println("key: " + key + ", " + fileMap.get(key));
}
}
}
file:
[file:\home\subn0de\test, file:\home\subn0de\test01, file:\home\subn0de\Downloads\asdf, file:\home\subn0de\Downloads\skydomes\5313_Filey.png, file:\home\subn0de\1\2\3\winzig]
untersuche parent: file:\home\subn0de
saved 2 childs for file:\home\subn0de
untersuche parent: file:\home\subn0de
parent schon bekannt, tschüss
untersuche parent: file:\home\subn0de\Downloads
saved 1 childs for file:\home\subn0de\Downloads
untersuche parent: file:\home\subn0de\Downloads\skydomes
saved 1 childs for file:\home\subn0de\Downloads\skydomes
untersuche parent: file:\home\subn0de\1\2\3
saved 1 childs for file:\home\subn0de\1\2\3
key: file:\home\subn0de\1\2\3, [file:\home\subn0de\1\2\3\winzig]
key: file:\home\subn0de\Downloads\skydomes, [file:\home\subn0de\Downloads\skydomes\5313_Filey.png]
key: file:\home\subn0de\Downloads, [file:\home\subn0de\Downloads\asdf]
key: file:\home\subn0de, [file:\home\subn0de\test, file:\home\subn0de\test01]