Solved

Copy a excel file by Java

Posted on 2007-03-22
3
446 Views
Last Modified: 2012-06-27
I have a xls sheet with Macro, Initially I want to use Java to copy the file. Since there is no copy method in java.io.File. I have to read it to BufferedReader and write it to new File by BufferedWriter. Codes are below:
But the new xls is corrupted if using following codes. Do you know some other way? I have an alternative:
I write a bat file; it says copy originalFileName newFileName. Then in Java program, I just call this bat file. But I do not know how to call a bat file in Java. I would put this to another question, since there are two questions here. Please also bother to answer another. So I could give points.




FileReader templateFile = null;
        try{
            templateFile= new FileReader("c:/temp/temp/temp/template.xls");
        }catch(FileNotFoundException fe){
            System.out.println("template file not found");
            System.exit(-1);
        }
        BufferedReader reader = new BufferedReader(templateFile);
       
        String inputLine;
        try
        {
            BufferedWriter writer = new BufferedWriter(new FileWriter("c:/temp/temp/temp/pastedName.xls"));
            while((inputLine = reader.readLine())!=null){
                writer.write(inputLine);
                writer.flush();
            }
            reader.close();
            writer.close();
        }
        catch (IOException e1)
        {    
            String msg = "An error occurred: " + e1.getMessage();
            System.out.println(msg);
            System.exit(-1);
        }
0
Comment
Question by:JianJunShen
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 18770485
Use BufferedOutputStream/InputStream, not Read/Writer
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 18770492
use something like the following

http://www.objects.com.au/java/qa/1579575043.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18770574
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
Arrays.asList  VS  ArrayList 4 45
fibonacci ten numbers 4 28
How to convert String matching to regex in java 4 21
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now