import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//简单的一个例子,来模拟输出
② java中读入和输出文本文件
/**
*测试3:从文本文件中读取数据
*/
staticvoidtestExample03(){
//1、在内存中打开要读取文件的字符流对象
try{
Readerreader=newFileReader("e:/ReadMe.log");
//2、从字符流中读取数据
//一次读取一个字符(麻烦)
/*intnum=reader.read();
System.out.println((char)num);
num=reader.read();
System.out.println((char)num);*/
//一次读取一个数组(必须确定数组的长度)
/*char[]cbuf=newchar[10];
reader.read(cbuf);
System.out.println(newString(cbuf));*/
//循环读取,一次就读一个
intch=reader.read();
StringBufferbuffer=newStringBuffer();
while(ch!=-1){//读取成功
buffer.append((char)ch);
ch=reader.read();
}
System.out.println(buffer.toString());
//3、关闭流
reader.close();
}catch(FileNotFoundExceptione){
System.out.println("要读取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件读取错误:"+e.getMessage());
}
}
/**
*测试4:向文本文件中写入数据
*/
staticvoidtestExample04(){
System.out.println("请输入内容:");
Stringtext=input.next();
try{
//1、打开流
Writerw=newFileWriter("e:/测试.txt",true);
//2、写入内容
w.write(text);
//3、关闭流
w.close();
}catch(IOExceptione){
System.out.println("文件写入错误:"+e.getMessage());
}
}
/**
*测试5:使用效率高的字符流读写数据
*/
staticvoidtestExample05(){
try{
//1、创建流对象
Readerreader=newFileReader("e:/ReadMe.log");
//构建高效流对象
BufferedReaderbuffReader=newBufferedReader(reader);
//2、读取一行字符串
Stringline=buffReader.readLine();
StringBufferbuffer=newStringBuffer();
while(line!=null){
buffer.append(line+" ");
line=buffReader.readLine();
}
System.out.println(buffer.toString());;
//3、关闭流
buffReader.close();
reader.close();
Writerw=newFileWriter("e:/NewReadMe.txt");
BufferedWriterbuffWriter=newBufferedWriter(w);
buffWriter.write(buffer.toString());
buffWriter.close();
w.close();
System.out.println("写入成功!");
}catch(FileNotFoundExceptione){
System.out.println("要读取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件读取错误:"+e.getMessage());
}
}
③ java中输出txt文件
不清楚楼主具体是哪里不懂,先给个大概思路,后台也就是action或servlet中利用java.io包把文本读出来,然后setAttribute()到页面上去,页面上textarea取set进去的值就ok了
写文件大同小异,流程相反
附上io的一个例子
final int BUFFER_LENGTH=1024;
public void formatFlie(String fileName) {
char[] c=new char[BUFFER_LENGTH];//buffered area
String inStr="";//read String
try {
BufferedReader readBuff=new BufferedReader(new FileReader(fileName));
while(readBuff.read(c,0,BUFFER_LENGTH)!=-1) {
//System.out.println("pos:"+read_pos);
for(int i=0;i<BUFFER_LENGTH;i++)
inStr+=c[i];
}
System.out.println(inStr);
readBuff.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
④ Java从命令行读取输入然后输出到文本文件的问题
importjava.io.*;
publicclassInputName{
publicstaticvoidmain(String[]args)throwsIOException{
System.out.println("请输入姓名(结束请敲两次回车键):");
InputStreamReaderkeyBoard=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(keyBoard);
FileWriterwriter=newFileWriter("name.txt",true);
Strings;
while((s=br.readLine())!=null){
writer.write(s);
}
writer.flush();
writer.close();
keyBoard.close();
br.close();
}
}
⑤ 如何用java输出txt文件
输入无需使用字节流,直接字符流读取即可。
privatevoidinput(StringfileName)throwsIOException{
try(BufferedReaderreader=newBufferedReader(newFileReader(fileName))){
Stringline;
while((line=reader.readLine())!=null){
System.out.println(line);
}
}
}
同样输出,只要把Input换成Output;
privatevoidoutput(StringfileName,Stringcontent)throwsIOException{
try(BufferedWriterwriter=newBufferedWriter(newFileWriter(fileName))){
writer.write(content);
writer.flush();
}
}
⑥ java如何把循环遍历结果输出到文本文档
首先,啊,我的眼睛!请学会截图,你的这三张图我一张都看不清!
然后我想了一下你的目的,你现在有一个学生信息数组,你是要把他们写到一个文件里是吧,这个过程叫做数据序列化或者持久化(其实文件中保存成json串或xml的形式更容易阅读数据和反序列化)因为看不清你的程序,所以我举了个例子给你看下,给你些思路。
我定义一个学生类,包括姓名和分数两个属性:
完整main函数代码:
public static void main(String[] args) throws Exception {
Student s1 = new Student("张三", 90);
Student s2 = new Student("李四", 59);
Student s3 = new Student("王五", 85);
Student[] students = new Student[]{s1, s2, s3};
String filePath = "d:\student.txt";
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileOutputStream = new FileOutputStream(filePath);
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
for (int i = 0; i < students.length; i++) {
bufferedWriter.write(students[i].getName() + " " + students[i].getGrade());
if (i < students.length - 1) {
bufferedWriter.newLine();
}
}
} finally {
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
⑦ 怎么用JAVA把Mysql数据库中的表的数据输出至文本文档中
首先导入 mysql-connector-java-5.1.45-bin.jar 包
代码如下:
importjava.io.*;
importjava.sql.*;
publicclassApp{
publicstaticvoidmain(String[]args){
try{
Class.forName("com.mysql.jdbc.Driver");
//数据库用户
Stringuser="root";
//数据库密码
Stringpassword="";
Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_sale",user,password);
Statementstmt=conn.createStatement();
//查询,从数据库db_sale的proct表中查询id,name,qty字段
ResultSetrs=stmt.executeQuery("SELECTid,name,qtyFROMproct");
//创建输出文件result.txt
Filefile=newFile("d://result.txt");
OutputStreamWriterwriter=newOutputStreamWriter(newFileOutputStream(file));
while(rs.next()){
writer.write(String.valueOf(rs.getLong(1))+" ");
writer.write(rs.getString(2)+" ");
writer.write(String.valueOf(rs.getInt(3)));
writer.write(" ");
//System.out.println(rs.getLong(1));
//System.out.println(rs.getString(2));
//System.out.println(rs.getLong(3));
}
writer.flush();
writer.close();
rs.close();
stmt.close();
conn.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
⑧ 怎么将一个java程序的结果输出到文本文档中,写一段代码,谢谢
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.Scanner;
publicclassOutToTxt{
;
privatestaticScannersc;
publicstaticvoidmain(String[]args){
=newFile("./Out.txt");
if(!out.exists()){
try{
out.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
}
}
try{
writer=newBufferedWriter(newFileWriter(out));
}catch(IOExceptione){
e.printStackTrace();
}
sc=newScanner(System.in);
System.out.println("请输入文本内容,输入exit结束:");
try{
writer.write("");//清空文本
Stringsplit="";
while(true){
Stringline=sc.nextLine();
if(line.equalsIgnoreCase("exit")){
break;
}
writer.append(split+line);
split=" ";
}
}catch(IOExceptione1){
e1.printStackTrace();
}finally{
if(null!=writer){
try{
writer.flush();
writer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
你可以运行后输入
Hello World!
This is my first application.
exit
⑨ java将文本文档信息输出显示在屏幕上!
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);//创建读入的buffer
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){//按行输出读取的内容
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\Apache\\htdocs\\res\\read.txt";//文件路径名称
readTxtFile(filePath);
}
}
复制粘贴自网上,添加了部分备注。。