❶ java如何将一个txt文件导出并显示
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class IOTexst {
public static void main(String[] arg){
String temp=null;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("d:/test.txt"));
while((temp=br.readLine())!=null){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
❷ java把运行结果输出到txt
这个最主要的就是万年历算法,网上一搜就有 输出保存到新建的Txt文件很容易就是IO写入操作例如:
BufferedWriterwriter=newBufferedWriter(newFileWriter(txt文件路径加路径名,
false));//true表示追加
writer.write(要写入的数据);
writer.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生成TXT文件
生成TXT的方法有很多的。常用位字节流和字符流
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class TextFileGenerator {
public static void main(String[] args) throws Exception {
method1();
method2();
}
private static void method1() throws Exception {
String txtContent = "Hello World!";
File file = new File("test1.txt");
FileWriter fw = new FileWriter(file);
fw.write(txtContent);
fw.close();
}
private static void method2() throws Exception {
String txtContent = "Hello World!";
File file = new File("test2.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(txtContent.getBytes());
fos.close();
}
}
❺ java输出到TXT文件时怎么加换行
java输出到txt的时候增加换行符的方法如下:
package
com.anjoyo.test;
import
java.io.FileWriter;
import
java.io.IOException;
public
class
TestFileWriter
{
public
static
void
main(String[]
args)
throws
IOException{
//\r\n为换行符
FileWriter
fw
=
new
FileWriter("D:\\1.txt");
//写入第一行换行
fw.write("第一行\r\n");
//或者获得系统换行符
String
str
=
"第二行"
+
System.getProperty("line.separator");
fw.write(str);
fw.write("第三行");
fw.close();
/*
*
windows下的文本文件换行符:\r\n
linux/unix下的文本文件换行符:\r
*
Mac下的文本文件换行符:\n
*/
}
}
❻ java生成txt文件 急急急!!!
package file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/** 用FileOutputStream类往指定文件中写入数据 */
public class FileOutputStreamTest {
public static void main(String[] args) {
FileOutputStream out = null;
try {
//step1: 创建一个向指定名的文件中写入数据的FileOutputStream
//第二个参数设置为true表示:使用追加模式添加字节
out = new FileOutputStream("D:\\IOTest\\dest.txt",true);
//step2: 写数据
out.write('#');
out.write("helloWorld".getBytes());
out.write("你好".getBytes());
out.write("\r\n".getBytes());//换行
out.write("网络新浪".getBytes());
//step3: 刷新此输出流
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { // 捕获IO异常
e.printStackTrace();
}finally{
if(out != null){
try {
out.close(); //step4: 关闭输出流
} catch (IOException e) {
e.printStackTrace();
} } } }}
给你看看我写的 参考下吧
❼ 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文件
importjavax.swing.*;
importjavax.swing.filechooser.*;
importjavax.swing.text.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
/**
*格子绘图演示
*@authorhardneedl
*/
{
=newDimension(600,400);
publicDimensiongetMinimumSize(){returnSIZE;}
publicDimensiongetMaximumSize(){returnSIZE;}
(){returnSIZE;}
publicStringgetTitle(){return"NotePadDemo";}
{
;
;
SaveAction(JTextComponentt){
super("保存...");
textComponent=t;
fileChooser=newJFileChooser(".");
fileChooser.setFileFilter(newFileNameExtensionFilter("文本文件","txt"));
}
publicvoidactionPerformed(ActionEvente){
if(fileChooser.showDialog(null,"保存")==JFileChooser.APPROVE_OPTION){
Filef=fileChooser.getSelectedFile();
try{
FileWriterfw=newFileWriter(f);
fw.write(textComponent.getText());
fw.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
privateJTextAreatextArea;
privateNotePadDemo()throwsHeadlessException{
super();
init();
addListeners();
doLay();
}
privatevoidinit(){
textArea=newJTextArea();
}
privatevoiddoLay(){
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);
JMenufileMenu=newJMenu("文件");
fileMenu.add(newSaveAction(textArea));
menuBar.add(fileMenu);
getContentPane().add(newJScrollPane(textArea),BorderLayout.CENTER);
pack();
setVisible(true);
}
privatevoidaddListeners(){
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
super.windowClosing(e);
}
});
}
publicstaticvoidmain(String...args){
SwingUtilities.invokeLater(NotePadDemo::new);
}
}
❾ 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文件的问题
我觉的你的问题在于在循环中一直调用response.getWriter().print();这句,计算机运行中response.getWriter()会不停的生成一个PrintWriter类的对象,导致堆空间在短时间内生成大量的对象,在垃圾回收器未来的及回收之前就内存溢出了。
建议修改:在循环外使用PrintWriter pw=response.getWriter();
循环内使用pw.print();方法。再试试看
如果你的list里放了太多的数据,这样自身就会内存溢出。list中的对象如果没内存溢出,再使用上面说的方法试,不要再用StringBuffer存list中的数据,StringBuffer存list内数据时也是占用内存的,这样你内存消耗的更快。
list最好分成多次存储对象。