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.
Re: hi, I need help for this code ...
Just use something like this:
Code :
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.
Re: hi, I need help for this code ...