PDA

View Full Version : کپی فایل از یک پوشه به پوشه ی دیگر.



ravand
جمعه 14 مهر 1391, 16:41 عصر
سلام
من خیلی سعی کردم تا یه برنامه ای بنویسم که یک فایل حالا با هر فرمتی رو از یک پوشه به پوشه ای دیگر در درایو دیگر کپی کنم ولی نتونستم هر چی هم جستجو کردم پیدا نکردم . هر چی پیدا میکردم متن داخل فایل رو کپی می کرد ولی خود فایل رو کپی نمیکرد.
متشکرم.

amin1softco
جمعه 14 مهر 1391, 20:42 عصر
وقتی یک فایل را از اول بخونی و در یک آدرس دیگه بنویسی همان عمل کپی انجام می شود!
در روش دیگه می تونی یکبار یک بچ فایل اجرا کنی که محتواش این باشه (یا xcopy):

copy "C:\run.jar" "C:\Users\username\AppData\Roaming\Microsoft\Window s\Start Menu\Programs\Startup\run.jar" /y

در روش دیگه می تونی با Runtime.getRuntime().exec کامند بالا رو اجرا کنید.
http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java
http://stackoverflow.com/questions/2389156/calling-win32-api-method-from-java
.
.
.

ravand
جمعه 14 مهر 1391, 20:59 عصر
از آدرسی که دادید هیچی دستگیرم نشد. و این کدی که گذاشتید رو اینطوری نوشتم که کار نکرد:
package testi;
import java.io.File;
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
copy "F:\java\bin\testi\dist\start.jar" "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\start.jar" ;
}
}

amin1softco
جمعه 14 مهر 1391, 21:05 عصر
عزیزمی :

String cmd = "copy C:\\run.jar C:\\Users\\username\\AppData\\Roaming\\Microsoft\\ Windows\\Start Menu\\Programs\\Startup\\run.jar /y"
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}

ravand
شنبه 15 مهر 1391, 07:29 صبح
این روشی که شما گفتید کار نکرد:
package testi;
import java.io.*;

public class Main {
public static void main(String[] args) {
String cmd = "copy C:\\dst.txt F:\\dst.txt /y";

try {

Runtime.getRuntime().exec(cmd);

} catch (IOException e) {

e.printStackTrace();

}

}
}
این روش رو خودم انجام دادم کار میکنه ولی نمیدونم چرا چیزی رو که میخوام به پوشه ی startup بریزم نمیشه. و اخطار میده:
package testi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws Exception {
Process process = Runtime.getRuntime().exec("xcopy /y c:\\src.txt C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\als.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("xcopy says: " + reader.readLine() + " " + reader.readLine());
OutputStream os = process.getOutputStream();
os.write("F".getBytes());
os.flush();
}
}

ravand
شنبه 15 مهر 1391, 08:47 صبح
این کد رو هم مشکلش رو حل کردم ولی نمیدونم چرا چیزی رو توی پوشه ی startup کپی نمیکنه!!
package testi;
import java.io.IOException;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
try {
Process process = Runtime.getRuntime()
//فراخوانی برنامه ی cmd
.exec("cmd.exe /c copy D:\\sorosh.txt D:\\testi\\test.txt");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}

ّّFardin
دوشنبه 17 مهر 1391, 20:10 عصر
این روشی که شما گفتید کار نکرد:
package testi;
import java.io.*;

public class Main {
public static void main(String[] args) {
String cmd = "copy C:\\dst.txt F:\\dst.txt /y";

try {

Runtime.getRuntime().exec(cmd);

} catch (IOException e) {

e.printStackTrace();

}

}
}
این روش رو خودم انجام دادم کار میکنه ولی نمیدونم چرا چیزی رو که میخوام به پوشه ی startup بریزم نمیشه. و اخطار میده:
package testi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws Exception {
Process process = Runtime.getRuntime().exec("xcopy /y c:\\src.txt C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\als.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("xcopy says: " + reader.readLine() + " " + reader.readLine());
OutputStream os = process.getOutputStream();
os.write("F".getBytes());
os.flush();
}
}


سلام
ميشه لطفا يه توضيح در مورد متد .write و .flush بديد ؟
كار اين 2 تا چيه ؟