输入无需使用字节流,直接字符流读取即可。
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把运行结果输出到txt
这个最主要的就是万年历算法,网上一搜就有 输出保存到新建的Txt文件很容易就是IO写入操作例如:
BufferedWriterwriter=newBufferedWriter(newFileWriter(txt文件路径加路径名,
false));//true表示追加
writer.write(要写入的数据);
writer.close();
⑶ JAVA如何调用txt文件 并用txt打印输出结果
import java.io.*;
public class Start
{
public void readAndRead()
{
try
{
FileReader fr = new FileReader("need.txt");//需要读取的文件路径
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while(s!=null)//如果当前行不为空
{
System.out.println(s);//打印当前行
s= br.readLine();//读取下一行
}
br.close();//关闭版BufferReader流
fr.close(); //关闭文件流
}catch(IOException e)//捕捉异常权
{
System.out.println("指定文件不存在");//处理异常
}
}
public static void main(String [] args)
{
Start start = new Start();
start.readAndRead();
}
}
⑷ java输出txt
在D盘新建两个文件test.txt,test1.txt
把内容拷到test中,test1为输出。。。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ZhiDao {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
String lineContent = null;
StringBuffer sb = new StringBuffer();
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("D:\\test.txt"));
pw = new PrintWriter("D:\\test1.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((lineContent = br.readLine()) != null) {
if(lineContent.equals("")){
sb.append("\r\n");
sb.append("\r\n");
}else{
for(int i = 0; i<lineContent.length(); i++){
char charContent = lineContent.charAt(i);
sb.append(charContent);
if(i != 0 && i%60 == 0){
sb.append("\r\n");
}
}
}
}
System.out.println(sb);
pw.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⑸ Java读取TXT文件并且输出~请各位大侠解答。小弟拜谢
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
publicclassReader{
publicstaticvoidmain(String[]args)throwsFileNotFoundException{
Scannerin=newScanner(newFile("dataIn.txt"));
Stringline;
//每次读取一行
List<String>result=newArrayList<String>();
while(in.hasNextLine()){
line=in.nextLine();
Stringstr[]=line.split(";");
result.add(str[1].split("=")[1]);
}
FiledataOut=newFile("dataOut.txt");
PrintWriterpw=newPrintWriter(dataOut);
for(Stringb:result){
System.out.println(b);
pw.println(b);
}
pw.close();
}
}
⑹ JAVA 如何输出数据到TXT文件内
package test;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
public class ReadColorTest {
/**
* 读取一张图片的RGB值
*
* @throws Exception
*/
public void getImagePixel(String image) throws Exception {
File fileCar = new File("D:\\car.txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("width=" + width + ",height=" + height + ".");
bos.write(("width=" + width + ",height=" + height + ".\n").getBytes());
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
bos.write(("minx=" + minx + ",miniy=" + miny + ".\n").getBytes());
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","+ rgb[1] + "," + rgb[2] + ")");
bos.write(("i=" + i + ",j=" + j + ":(" + rgb[0] + ","+ rgb[1] + "," + rgb[2] + ")\n").getBytes());
}
}
}
/**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
rc.getImagePixel("D:\\car.jpg");
}
}
⑺ java实现将结果输出到txt
遍历所有数组,然后用FileInputStream()
FileOutputStream fos = new FileOutputStream("d://data.txt",true);
⑻ Java中怎么写数据输出为.txt格式的文件
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中输出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 数据输出到txt文件
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();
}
}
//简单的一个例子,来模拟输出