用JAVA怎样才能将数据输出到文件的指定位置

2024-11-01 21:31:39
推荐回答(4个)
回答(1):

参考这程序吧
/*
* c7:IODemo4.java 1.0 05/02/10
* author:Leo
* This program is protected by copyright laws.
* Reader from and write to files.
* 字符流和字节流类似,但效率更高.有可能的情况下,尽量使用reader和writer
* 一般只要把Reader替换为InputStream,用Writer 替换OutputStream,当然还要用Char替换Byte.
* 用reader和writer可以一次读一行,而且没有类型转换的麻烦 .
*/

package myprojects.readerwriter;

import java.io.*;

class IODemo4
{
public static void main(String args[])
{
System.out.println("Starting reader...");
String s = new String();
String s2=new String();
try
{
BufferedReader in = new BufferedReader(new FileReader("d:\\classtry\\My.class"));
PrintWriter out = new PrintWriter(new FileWriter("d:\\classtry\\destnation.xls"));
//BufferedReader in2 = new BufferedReader(new FileReader("d:\\javatry\\classtry\\destnation.xls"));

//把下面的字符串写入到文件的第一行.

//out.println("the following is from source");
//下面的循环语句读入一行内容的同时进行写入.
while((s=in.readLine()) != null)
{
System.out.println(s);
//char ss[]=s.toCharArray();
out.println(s+"\t");
}
//in2.close();
in.close();
out.close();//关闭文件.

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

回答(2):

参考这程序吧 /* * c7:IODemo4.java 1.0 05/02/10 * author:Leo * This program is protected by copyright laws. * Reader from and write to files. * 字符流和字节流类似,但效率更高.有可能的情况下,尽量使用reader和writer * 一般只要把Reader替换为InputStream,用Writer 替换OutputStream,当然还要用Char替换Byte. * 用reader和writer可以一次读一行,而且没有类型转换的麻烦 . */ package myprojects.readerwriter; import java.io.*; class IODemo4 { public static void main(String args[]) { System.out.println("Starting reader..."); String s = new String(); String s2=new String(); try { BufferedReader in = new BufferedReader(new FileReader("d:\\classtry\\My.class")); PrintWriter out = new PrintWriter(new FileWriter("d:\\classtry\\destnation.xls")); //BufferedReader in2 = new BufferedReader(new FileReader("d:\\javatry\\classtry\\destnation.xls")); //把下面的字符串写入到文件的第一行. //out.println("the following is from source"); //下面的循环语句读入一行内容的同时进行写入. while((s=in.readLine()) != null) { System.out.println(s); //char ss[]=s.toCharArray(); out.println(s+"\t"); } //in2.close(); in.close(); out.close();//关闭文件. } catch(Exception e) { e.printStackTrace(); } } }

回答(3):

你想输出到什么地方 你看IO那这方面的

回答(4):

io、sql