[JAVA] Manipuleren van files

Pagina: 1
Acties:

  • Banaan
  • Registratie: Maart 2000
  • Laatst online: 08-09 11:34
Ik heb een bestand wat d.m.v. Java gemanipuleerd moet worden (copy, delete, rename). Nu heb ik al wat rond gezocht op internet en in wat boeken hier, maar ik kan dat niet zo snel vinden. Heeft iemand enig idee hoe ik dat kan doen? Want java.io maakt het wel mogelijk om te schrijven naar en te lezen van bestanden, maar heb nog niet gezien hoe je die bestanden kan manipuleren.

Mocht dit nou niet mogelijk zijn (lijkt me sterk maar goed) is het dan misschien wel mogelijk om via Java een batch-bestand (met daarin die manipulaties) uit te voeren?

  • Tomatrix
  • Registratie: Juni 1999
  • Laatst online: 27-02-2025
Kijk 's naar de File klasse in het java.io package.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Kijk eens in de API documentatie van java.io.File. Je ziet hier van alles voor deleten, renamen etc.

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Banaan
  • Registratie: Maart 2000
  • Laatst online: 08-09 11:34
Ah.. wat dom van me dat me dat niet is opgevallen. Maar heb het nu voor 2/3 voor elkaar. Delete en rename werkt nu, maar ik kan binnen de File-class niks vinden voor het kopieren van een bestand. Enig idee hoe dit dan zou moeten?

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Kopieren is geen atomaire operatie en dus is daar geen functie voor java.io.File. Je moet dit gewoon implementeren mbv een InputStream en een OutputStream :) .

Mats-mode:
code:
1
2
3
4
5
6
7
8
9
10
11
12
    public static void copy(File source, File target) throws FileNotFoundException, IOException
    {
        FileOutputStream write = new FileOutputStream(target); 
        FileInputStream  read  = new FileInputStream(source);
      
      DefaultStreamWriteTools tools = new DefaultStreamWriteTools();      
     
        tools.write(write, read);

        write.close();
        read.close();
    }


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package org.pandoramix.io;

import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;

import java.util.Collection;
import java.util.Iterator;

public interface StreamWriteTools
{
    public void setCloseStream(boolean value);
    public void write(OutputStream stream, String content)     throws IOException;
    public void write(OutputStream stream, byte[] content)     throws IOException;
    public void write(OutputStream stream, Collection content) throws IOException;
    public void write(OutputStream stream, Iterator content)   throws IOException;
    public void write(OutputStream stream, InputStream input)  throws IOException;   
    public void write(OutputStream stream, InputStream input, int bufferSize)  throws IOException;
}


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.pandoramix.io;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.InputStream;
import java.io.IOException;

import java.util.Collection;
import java.util.Iterator;

public class DefaultStreamWriteTools implements StreamWriteTools
{
    private boolean _closeStream;
    
    public DefaultStreamWriteTools()
    {
      super();
      _closeStream = false;
    }

    public void setCloseStream(boolean value)
    {
      _closeStream = value;
    }

    private void close(OutputStream stream) throws IOException
    {
      if(_closeStream)
      {
        stream.close();   
      }     
    }     
      
    public void write(OutputStream stream, String content)     throws IOException
    {
        write(stream, content.getBytes());
    }
      
    public void write(OutputStream stream, byte[] content)     throws IOException
    {
      stream.write(content);
      close(stream);
    }     
    
    public void write(OutputStream stream, InputStream input)  throws IOException
    {    
        write(stream, input, 4096);    
    }
    
    public void write(OutputStream stream, InputStream input, int bufferSize)  throws IOException
    {    
        byte[] buffer = new byte[bufferSize];
        int n;

        while ((n = input.read(buffer, 0, buffer.length)) != -1) 
        {
            stream.write(buffer, 0, n);
        }

        stream.flush();
      close(stream);
    }
   
    public void write(OutputStream stream, Collection content) throws IOException
    {
        write(stream, content.iterator());    
    }     
    
    public void write(OutputStream stream, Iterator content)   throws IOException
    {
      OutputStreamWriter writer = new OutputStreamWriter(stream);
      
        while(content.hasNext())
        {
            writer.write((String)content.next());
        }
            
      close(stream);
    }     
}

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Banaan
  • Registratie: Maart 2000
  • Laatst online: 08-09 11:34
Tja.. wat valt hier nog op te zeggen.. je bent mn held! :)
Het werkt nu helemaal, dus onwijs bedankt!! :*
Pagina: 1