JAVA中,从一个文件中读出的数据怎么写入另一个文件
答案:4 悬赏:30 手机版
解决时间 2021-03-16 14:23
- 提问者网友:龅牙恐龙妹
- 2021-03-16 07:32
public static void fileRead(){ File f=new File("G:/森云/测试文件。txt"); RandomAccessFile rdf=null; try{ rdf=new RandomAccessFile(f,"rw"); rdf.skipBytes(8); byte[] b=new byte[8]; rdf.read(b); String str=new String(b); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public static void fileWrite()throws FileNotFoundException,IOException{ File f=new File("G:/森云/测试文件1。txt"); RandomAccessFile rdf=null; rdf=new RandomAccessFile(f,"rw"); }我怎么将读文件的读出的str传入到写文件的方法中去
最佳答案
- 五星知识达人网友:行雁书
- 2021-03-16 08:47
public static void testRead(String oldFileName,String newFileName) throws IOException{
FileOutputStream fos=new FileOutputStream(new File(newFileName));
RandomAccessFile raf=new RandomAccessFile(new File(oldFileName), "rw");
fos.write(raf.read(new byte[8]));
fos.flush();
fos.close();
raf.close();
}
public static void fileWrite() throws FileNotFoundException, IOException {
testRead("G:/森云/测试文件1。txt","G:/newFile.txt");
}
全部回答
- 1楼网友:一叶十三刺
- 2021-03-16 11:25
RandomAccessFile
可读、可写的呀,另打开一个写入就好了
- 2楼网友:孤独的牧羊人
- 2021-03-16 10:21
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
public class usebuffer
{
public static void main(string args[]) throws ioexception
{
fileinputstream fis;
fileoutputstream fos;
bufferedinputstream bis;
bufferedoutputstream bos;
int i;
try
{
fis=new fileinputstream("text1.txt"); //文件输入流
bis=new bufferedinputstream(fis); //连接带缓冲的输入流
fos=new fileoutputstream("text2.txt"); //文件输出流
bos=new bufferedoutputstream(fos); //连接带缓冲的输出流
i=bis.read(); //读数据
while(i!=-1)
{
bos.write(i); //写数据
bos.flush(); //强制输出
i=bis.read();
}
fis.close();
fos.close();
bis.close();
bos.close();
}
catch(ioexception e)
{
system.out.println("do not find the file");
}
}
}
参考
- 3楼网友:刀戟声无边
- 2021-03-16 09:39
有简单的方法,给你个例子32313133353236313431303231363533e4b893e5b19e31333332626633
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
String srcFile = "D:/a.txt";
String toFile = "D:/b.txt";
try {
String result = read(srcFile);
write(result, toFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String read(String srcFile) throws FileNotFoundException {
Scanner in = new Scanner(new File(srcFile));
String result = "";
while (in.hasNextLine()) {
result += in.nextLine() + "\r\n";
}
in.close();
return result;
}
private static void write(String result, String toFile) throws Exception {
Writer w = new FileWriter(new File(toFile));
w.write(result);
w.flush();
w.close();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯