How make file system listing in Java Swing -
i need index whole file system, , convert xml. created code, produce out of memory error (heap). question how avoid error. due creating file1 object or vector match have set virtual memory again produce error @ 973.
`
import java.util.vector; import java.io.bytearrayinputstream; import java.io.objectinputstream; public class filewalk { vector<file1> vs= new vector<file1>(); public void walk( string path ) { file root = new file( path ); file[] list = root.listfiles(); if (list == null) return; ( file f : list ) { if ( f.isdirectory() ) { walk( f.getabsolutepath() ); } else { file1 fv=new file1(f.getabsolutefile().tostring(),f.length()); --we change next string-- //mainrootelement.appendchild(getfile (doc, num++, f.getname(), f.length(), f.getabsolutefile());// } } } public static void main(string[] args) throws exception { filewalk fw = new filewalk(); file [] disks = file.listroots(); ( file d : disks ) fw.walk(d.tostring()); //bytearrayinputstream baos = new bytearrayinputstream(); //objectinputstream oos = new objectinputstream(baos); //oos.writeobject(fw.vs); //oos.close(); } } class file1 { file1 (string path1, long size1) { this.path=path1; this.size=size1;} string path; long size;}
`
exception in thread "main" `java.lang.outofmemoryerror: java heap space @ java.io.winntfilesystem.list(native method) @ java.io.file.list(file.java:973) @ java.io.file.listfiles(file.java:1051) @ listing.filewalk.walk(filewalk.java:8) @ listing.filewalk.walk(filewalk.java:12) @ listing.filewalk.walk(filewalk.java:12) @ listing.filewalk.walk(filewalk.java:12) @ listing.filewalk.walk(filewalk.java:12)
at listing.filewalk.main(filewalk.java:24)
i try create big vector pass swing jtable show @ least part of file(autonumber, name, size, path). anyway during uindexing need assign unique number , save something. maybe need use direct writing jdbc table through add record absolve memory issues? anyway if convert such properties xml tag need save big xml file search after in name these files. how resolve issue? idea of vector of file1's correct? how space take whole indexing?
can propose that. added inserting records aforementioned code. due avoid memory crash adding every file property created database. not know can use in sql statement variables. created using such 1 f.method(). id possible avoid complexities craete nethods second code --one creating db , table , second inserting records , in way include methods. , of cause not know jow shape try , catch rules jdbc pieces of code.
`import java.sql.*; import java.io.file; public class filewalk { int i=0; static final string jdbc_driver = "com.mysql.jdbc.driver"; static final string db_url = "jdbc:mysql://localhost/students"; static final string user = "root"; static final string pass = ""; connection conn = null; statement stmt = null; //step 2: register jdbc driver class.forname("com.mysql.jdbc.driver"); //step 3: open connection system.out.println("connecting selected database..."); conn = drivermanager.getconnection(db_url, user, pass); system.out.println("connected database successfully..."); //step 4: execute query system.out.println("creating table in given database..."); stmt = conn.createstatement(); string sq = "create database filess"; stmt.executeupdate(sq); string sql1 = "create table system1 " + "(id integer not null, " + " name varchar(255), " + " path varchar(255), " + " size integer, " + " primary key ( id ))"; stmt.executeupdate(sql1); public void walk( string path ) { file root = new file( path ); file[] list = root.listfiles(); if (list == null) return; ( file f : list ) { if ( f.isdirectory() ) { walk( f.getabsolutepath() ); } else { string sql2 = "insert system1" + "values (i++,f.getname(), f.getabsolutefile().tostring(),f.length())"; stmt.executeupdate(sql2); } } try{ if(stmt!=null) conn.close(); }catch(sqlexception se){ }// nothing try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); }//end try }//end try system.out.println("goodbye!"); }//end main } public static void main(string[] args) { filewalk fw = new filewalk(); file [] disks = file.listroots(); ( file d : disks ) fw.walk(d.tostring()); } } `
Comments
Post a Comment