java copy file skip first line

package readline;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Test {

public static void main(String[] args) throws Exception {
String somepath = "d:/f1.txt";

writeFile("d:/line0.txt","11111113232");
writeFile("d:/line1.txt","11111113232\n11111113232\n11111113232\n11111113232");
writeFile("d:/line1lf.txt","11111113232\n11111113232\n11111113232\n11111113232");
writeFile("d:/line1crlf.txt","11111113232\r\n11111113232\r\n11111113232\r\n11111113232");

somepath = "d:/line0.txt";
copyLine(somepath,somepath+".tmp");
somepath = "d:/line1.txt";
copyLine(somepath,somepath+".tmp");
somepath = "d:/line1lf.txt";
copyLine(somepath,somepath+".tmp");
somepath = "d:/line1crlf.txt";
copyLine(somepath,somepath+".tmp");

}

public static void writeFile(String fileName , String content) throws FileNotFoundException{
try (PrintStream out = new PrintStream(new FileOutputStream(fileName))) {
   out.print(content);
   out.close();
}
}

public static void copyLine(String f1,String f2) throws Exception{
File inputFile = new File(f1);
File tempFile = new File(f2);

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

InputStreamReader isr = new InputStreamReader(new FileInputStream(inputFile));

String currentLine;
if((currentLine = reader.readLine()) != null){
int linefeedPosition = currentLine.length();

FileInputStream from = null; // Stream to read from source
   FileOutputStream to = null; // Stream to write to destination
 
     from = new FileInputStream(f1); // Create input stream
     to = new FileOutputStream(f2); // Create output stream
   
     byte[] buffer = new byte[linefeedPosition];
     int bytes_read; // How many bytes in buffer
     if((bytes_read = from.read(buffer)) != -1){
   
     }
     buffer = new byte[1];
     if((bytes_read = from.read(buffer)) != -1){
    System.out.printf(""+(int)buffer[0]);
    if('\r' == buffer[0]){
    System.out.printf(f1+":캐리지 리턴 검출됨");
    if((bytes_read = from.read(buffer)) != -1){
    System.out.printf(""+(int)buffer[0]);
    if('\n' == buffer[0]){
    System.out.println(f1+":라인피드 검출됨");
    }
     }
    }
     }else{
     //throw new Exception();
     return;
     }
   
   
     buffer = new byte[4096]; // To hold file contents
   

     // Read a chunk of bytes into the buffer, then write them out,
     // looping until we reach the end of the file (when read() returns
     // -1). Note the combination of assignment and comparison in this
     // while loop. This is a common I/O programming idiom.
     while ((bytes_read = from.read(buffer)) != -1)
       // Read until EOF
       to.write(buffer, 0, bytes_read); // write
 

}

writer.close();
reader.close();
//boolean successful = tempFile.renameTo(inputFile);
}

public static void copyLine2(String f1,String f2) throws Exception{
File inputFile = new File(f1);
File tempFile = new File(f2);

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));


String currentLine;
if((currentLine = reader.readLine()) != null){

while((currentLine = reader.readLine()) != null) {
   writer.write(currentLine + System.getProperty("line.separator"));
}
}

writer.close();
reader.close();
//boolean successful = tempFile.renameTo(inputFile);
}

}

댓글