Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: hi, I need help for this code ...

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post hi, I need help for this code ...

    sorry for posting this I dont know how to delete this thread ... sorry

    hi guys I need this code to ask d user to ENTER the FILE Name to be copied and what's the name of the new file where it will be copied.Thanks :]


    import java.io.File;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    public class copybytes {

    public void copyFile(String fromFile, String toFile) {

    FileInputStream fin = null;
    FileOutputStream fout = null;

    try {

    File file = new File(fromFile);
    if (!file.exists() || !file.isFile()) {
    System.out.println("Could not perform operation, file doesn\'t exist");
    return;
    }

    fin = new FileInputStream(file);
    fout = new FileOutputStream(toFile);


    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fin.read(buffer)) > 0) {
    fout.write(buffer, 0, bytesRead);
    }
    }
    catch (IOException ioe) {
    ioe.printStackTrace();
    }
    finally {
    try {
    if (fin != null)
    fin.close();
    if (fout != null)
    fout.close();
    }
    catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    copybytes fileutil = new copybytes();
    fileutil.copyFile("F:\\\\rica.txt",
    "F:\\\\muah.txt");
    }
    }

    this code is not mine I just found this on d internet coz im looking for a copy byte codes.
    Last edited by ricaclaire16; February 26th, 2012 at 01:03 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hi, I need help for this code ...

    Just use something like this:
    public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the destination file name");
    String dest = input.nextLine();
    System.out.println("Enter the source file name");
    String source = input.nextLine();
    if(copy(new File(dest), new File(source))){
    System.out.println("Successfully copied");
    }
    }
     
    public static boolean copy(File dest, File src){
    boolean copied = true;
    String copyText = "";
    BufferedWriter writer;
    if(!dest.exists() && !src.exists()){
    copied = false;
    }else{
    try{
    Scanner reader = new Scanner(src);
    while(reader.hasNextLine()){
    String line = reader.nextLine();
    if(line != null){
    copyText += copyText+"\n";
    }
    }
    }catch(Exception e){
     
    }
    try{
    writer = new BufferedWriter(new FileWriter(dest, true));
    writer.newLine();
    writer.write(copyText);
    }catch(Exception e){
     
    }finally{
    try{
    writer.flush();
    writer.close();
    }catch(Exception e){
     
    }
    }
    }
    }

    Something like that.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: hi, I need help for this code ...

    ricaclaire16, please read this: http://www.javaprogrammingforums.com...e-posting.html

    SupportIsPower, please read this: http://www.javaprogrammingforums.com...n-feeding.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM