java programming source code file operations thought IO8

2010-05-06  来源:本站原创  分类:Java  人气:152 

Explore channels and buffers

package com.dirlist;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.io.*;

public class ChannelCopy {
        private static final int BSIZE=1024;
        public static void main(String[] args) throws IOException {
                if(args.length!=2){
                        System.out.println("arguments: sourcefile destfile");
                        System.exit(1);
                }
                FileChannel in=new FileInputStream(args[0]).getChannel(),out=new FileOutputStream(args[1]).getChannel();
                ByteBuffer buff=ByteBuffer.allocate(BSIZE);
                // Once the call  read() To tell the FileChannel to  ByteBuffer Store the byte, you must call the buffer  flip(), Let it well let others prepare for reading bytes
                while(in.read(buff)!=-1){
                        buff.flip();
                        out.write(buff);
                        // If you intend to use a buffer to perform further  read() Operation, we have to call  clear() Method
                        buff.clear();// Clear the buffer-the location is set to zero  , Limit is set to the capacity, and discard flag  .
                }
        }

}

package com.dirlist;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.io.*;

public class GetChannel {

        /**
         *  New i/o channels and buffer
         */
        private static final int BSIZE=1024;
        public static void main(String[] args) throws IOException {
                //FileChannel Write for reading.  . Mapping and operational files of the channel  .
                FileChannel fc=new FileOutputStream("data.txt").getChannel();
                //ByteBuffer Byte buffer  .wrap() An array of bytes into the buffer packing  .
                // The new buffer is determined by the given byte array support  : In other words, the buffer modification will result in the array to modify  , Catch the new buffer capacity and bounds will be   array.length, Its position is zero, it is indeterminate  . Its underlying implementation array is an array for a given, and the array offset will be zero  .

                fc.write(ByteBuffer.wrap("Some text ".getBytes()));
                fc.close();
                fc=new RandomAccessFile("data.txt","rw").getChannel();
                //position() Set the location of the files for this channel  .
                // The location is set to a value greater than the value of the current size of the file is valid, but this does not change the size of the file  . Later on, an attempt was made to this location read bytes will return immediately at the end of the file is reached; later attempted to indicate the position of writing bytes will cause the file to expand the  , To accommodate the new bytes  : In the past the end of the file and the new write bytes between byte value is not specified  .
                //size() Returns the channel file's current size  .
                System.out.println(fc.size());
                fc.position(fc.size());
                fc.write(ByteBuffer.wrap("Some more ".getBytes()));
                fc.close();
                fc=new FileInputStream("data.txt").getChannel();
                // allocate(int capacity) Allocates a new buffer of bytes  .
                ByteBuffer buff=ByteBuffer.allocate(BSIZE);
                fc.read(buff);
                //flip() Reverse the buffer. first set limits on the current position  , And then the position is set to zero if the tag has already been defined  , The drop the tag  .
                buff.flip();
                //hasRemaining() Judgement in the current position and restrictions for any element  .
                while(buff.hasRemaining()){
                        System.out.print((char)buff.get());
                }

        }

}
package com.dirlist;

import java.nio.channels.FileChannel;
import java.io.*;
public class TransferTo {

        /**
         *  How to add a channel to another channel connection
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                if(args.length!=2){
                        System.out.println("arguments : sourfile destfile");
                        System.exit(1);
                }
                FileChannel in=new FileInputStream(args[0]).getChannel(),out=new FileOutputStream(args[1]).getChannel();
                in.transferTo(0, in.size(), out);
                // Or      out.transferFrom(in, 0, in.size());

        }

}
相关文章
  • java programming source code file operations thought IO8 2010-05-06

    Explore channels and buffers package com.dirlist; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.io.*; public class ChannelCopy { private static final int BSIZE=1024; public static void main(String[] args) throws IOExce

  • java programming source code file operations thought IO6 2010-05-05

    package com.dirlist; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; public class FileOutputShortcut { /** * The shortcut text file output */ static String file="FileOutputShortcut.o

  • java programming source code file operations thought IO5 2010-05-05

    package com.dirlist; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedInputFile { // Buffered input file public static String read(String filename) throws IOException{ //BufferedReader From ch

  • java programming source code file operations thought IO10 2010-05-08

    package com.dirlist; import java.nio.*; public class IntBufferDemo { /** * View buffer, use view buffer encapsulates the byte buffer */ private static final int BSIZE=1024; public static void main(String[] args) { ByteBuffer bb=ByteBuffer.allocate(BS

  • java programming source code ideology IO3 2010-05-03

    Using strategy design pattern to conduct directory traversal and file filtering package net.mindview.util; import java.io.File; import java.io.IOException; / / Strategy design pattern public class ProcessFiles ( public interface Strategy ( void proce

  • Excellent site source. Centralize programming source code download site 2010-11-12

    Excellent web site source code, programming source code download site centralization 1.51 Source: http://www.51aspx.com/ 2. Source Home: http://www.codejia.com/ 3. Source Network: http://www.codepub.com/ 4. Shrimps off Source: http://www.xkxz.com/ 5.

  • How effectively prevent the Java program source code was peeping? 2010-06-28

    Java program's source code is easy for people to peep, as long as an anti-compiler, any person can analyze other people's code. This article discusses how to modify the original program without the case, through encryption technology to protect the s

  • A good reference for learning network programming source code (complete code) 2010-03-27

    Good reference for our study has an unexpected effect, the source code easy to understand, and is object-oriented, for C + + beginners, very easy to understand. Not be introduced here, anyway, many people like to see source. I have tested can be comp

  • opencsv open source CSV file operations package Introduction 2010-04-13

    opencsv is a very simple csv (comma-separated values) parser library for Java. It was developed because all of current csv parsers I've come across don't have commercial-friendly licenses. opencsv supports all the basic csv-type things you're likely

  • java file operations to read and write basic 2011-01-11

    package com.hzsuwang.java; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java

  • Deep understanding of Java programming examples 7 2010-06-22

    1. Read the following code to answer questions (the first Java program, understand the PATH and CLASSPATH, learn to use javac, and java command) view plaincopy to clipboardprint? package cn.edu.uibe; public class HelloWorld ( public static void main

  • Ruby On Rails-2.0.2 source code analysis (1)-Rails Start 2010-11-09

    Preface This article is for Ruby On Rails 2.0.2 source code analysis, learning and research. The tools used NetBean 6.1 Beta, WEBRick, SciTE, ruby-debug-base (0.10.0), ruby-debug-ide (0.1.10). Ruby version is 1.8.6. How analysis should be summarized,

  • Compiled with the source code on the installation MYSQL5.5 to ubuntu10.10 2011-04-05

    http://forum.ubuntu.org.cn/viewtopic.php?t=319886 Compiled with the source code on the installation MYSQL5.5 to ubuntu10.10 [Notes] I first training beginner, badly written, forgive me - today soak up the afternoon forum, could not find the appropria

  • Java source code and recommended books to read! 2009-11-16

    1. Java language based on On the basis of Java language learning books, I am sure you will recommend Bruce Eckel's "Thinking in Java". It is written in a very profound technical books, Java language is not based on any other part of the capital

  • JSON for Java source code 2010-03-29

    Look at the source code is distributed together with you to explore the inadequacies of the wing please. 1. Test file: test.json /** * Test JSON * */ { id:-NaN, //ID $name_1:"johnson\" Lee \"+\"test\"", // Name married:false,

  • MyEclipse 6 Java development of Chinese language tutorial 1-18 s free download with source code 2010-03-29

    MyEclipse java development of Chinese language tutorial, this writing is still good, is a combination of MyEclipse and java terms, and to Zhang Screenshot: Chapter I: Installation and Configuration Development Environment Chapter II: Developing the f

  • JAVA source code of arbitrary structure builder CodeMaster v1.0 Beta 2009-01-27

    Download See attachment struts attached minimum system to run. CodeMaster is an intelligent code generation gadgets, using pure java development, small and simple but in many cases very useful, in theory, any structure can generate java source code,

  • How to build a robust code, java programming rules 30 (1) 2010-04-07

    How to build a robust code, java programming rules 30 (1) Source: Network reproduced :2009-6-15 This is some pretty good advice! Each rule is very weighty! Is a summary of long-term accumulation of experience, hoping to help you make your high-qualit

  • Android source code editor with all the operations related to introduction 2010-12-02

    Android source code editor with all the operations related to introduction Android source code related to Note: The following is carried out in the ubuntu 10.04. The birth of this article, my internship at the Meizu learned the past two months, who h

  • JAVA write file operations Gadgets 2010-12-13

    Long time no written code, very, very strange, a lot of things are forgotten, wrote a small tool that small problem there, get me a whole night, and later that knows the problem, but it still is in view of solving complex, thought did not need a gadg