利用流,输出流,先创建文件,然后利用输出流输出到word文档中。以下程序仅供参考://建立一个文件,向其中写入多行数据,然后读出来打印到显示器上
import java.io.*;
import java.util.*;
class TestBufferedOutputStream
{
public static void main(String args[]) throws IOException
{
File file = new File("a.doc");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
String str;
Scanner sc = new Scanner(System.in);
System.out.println("Please Input the 1 Line:");
str=sc.nextLine();
bos.write(str.getBytes());
bos.flush();
boolean bool=true;
while(bool==true)
{
for(int i=2; i<6; i++)
{
System.out.println("继续(Y)停止(N)");
str=sc.nextLine();
if(str.equals("Y"))
{
bos.write('\n');
System.out.println("Please Input the "+i+" Line:");
str=sc.nextLine();
bos.write(str.getBytes());
bos.flush();
}
if(str.equals("N"))
{
System.exit(-1);
}
}
}
bos.close();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] byt = new byte[1000];
bis.read(byt);
System.out.println("The Fllowing is Output Contents!");
System.out.println(new String(byt));
bis.close();
}
}
import java.io.*;
import java.util.*;
class TestBufferedOutputStream
{
public static void main(String args[]) throws IOException
{
File file = new File("a.doc");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
String str;
Scanner sc = new Scanner(System.in);
System.out.println("Please Input the 1 Line:");
str=sc.nextLine();
bos.write(str.getBytes());
bos.flush();
boolean bool=true;
while(bool==true)
{
for(int i=2; i<6; i++)
{
System.out.println("继续(Y)停止(N)");
str=sc.nextLine();
if(str.equals("Y"))
{
bos.write('\n');
System.out.println("Please Input the "+i+" Line:");
str=sc.nextLine();
bos.write(str.getBytes());
bos.flush();
}
if(str.equals("N"))
{
System.exit(-1);
}
}
}
bos.close();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] byt = new byte[1000];
bis.read(byt);
System.out.println("The Fllowing is Output Contents!");
System.out.println(new String(byt));
bis.close();
}
}
http://www.360doc.com/content/14/0224/11/13247663_355234675.shtml
http://blog.csdn.net/jiutianhe/article/details/25287639