use FileChannel and ByteBuffer : ByteBuffer « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » ByteBufferScreenshots 
use FileChannel and ByteBuffer
 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
  static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(1024);

    while (true) {
      int ret = inc.read(bb);
      if (ret == -1
        break;
      bb.flip();
      outc.write(bb);
      bb.clear()
    }
  }
}

   
  
Related examples in the same category
1. View buffers
2. Use NIO to read a text file.
3. Create a read-only memory-mapped file
4. Create a read-write memory-mapped file
5. Create a private (copy-on-write) memory-mapped file.
6. Fast Copy File
7. A ByteBuffer is a fixed-capacity buffer that holds byte values.
8. Create a ByteBuffer using a byte array
9. Create a non-direct ByteBuffer with a 10 byte capacity
10. Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.
11. Get the ByteBuffer's capacity
12. Use the absolute get().
13. Set the position
14. Use the relative get()
15. Get remaining byte count in a ByteBuffer
16. Set the limit for ByteBuffer
17. This convenience method sets the position to 0
18. Putting Bytes into a ByteBuffer
19. Converting Between a ByteBuffer an a Byte Array
20. Retrieve bytes between the position and limit
21. Retrieve all bytes in the buffer
22. Get and Set char type data in a ByteBuffer
23. Get and Set short type data in a ByteBuffer
24. Get and Set int type data in a ByteBuffer
25. Get and Set long type data in a ByteBuffer
26. Get and Set float type data in a ByteBuffer
27. Get and Set double type data in a ByteBuffer
28. Create a character ByteBuffer
29. Create a short ByteBuffer
30. Create an integer ByteBuffer
31. Create a long ByteBuffer
32. Create a float ByteBuffer
33. Create a double ByteBuffer
34. Get a substring
35. Using a ByteBuffer to Store Strings
36. Get default byte ordering
37. Put a multibyte value
38. Set to little endian
39. Determining If a ByteBuffer Is Direct
40. Reading from a Channel with a ByteBuffer
41. Writing and Appending a ByteBuffer to a File
42. Convert interchangeably between a ByteBuffer and a byte array
43. Create a ByteBuffer
44. Put bytes into a ByteBuffer
45. How to get bytes from a ByteBuffer
46. Write with ByteBuffer
47. Applying Regular Expressions on the Contents of a File
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.