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 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文件里,或者word文档里!
给你一点关键部分的提示
你可能需要用到apache zip这个组件
// zipFileName为要解压缩的zip为文件名,例:c:\\filename.zip
// outputDirectoty为解压缩后文件名,例:c:\\filename
public void unZip(String zipFileName, String outputDirectory)
throws Exception {
InputStream in = null;
FileOutputStream out = null;
try {
zipFile = new ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
// org.apache.tools.zip.ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("创建目录:" + outputDirectory
+ File.separator + name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
// System.out.println("测试文件1:" +fileName);
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1, fileName
.length());
}
try {
f = new File(outputDirectory + File.separator
+ zipEntry.getName());
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
byte[] by = new byte[100000];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
} catch (Exception ee) {
} finally {
if (in != null) {
in.close(); //解压完成后注意关闭输入流对象
}
if (out != null) {
out.close(); //解压完成后注意关闭输出流对象
}
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close(); //解压完成后注意关闭apache自带zip流对象
}
}
❹ 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文件,如何实现换行
1 实现的方式总共有3中, 1)原始的 \r\n 方式
2)BufferedWriter的newline()方法
3) System.getProperty()方法
下面使用代码逐一展示 , 写到.txt文件实现换行
2 使用java中的转义符"\r\n":
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str="aaa";
str+="\r\n";
fos.write(str);
3 BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline(); //实现换行
4 System.getProperty()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str = "aaa"+System.getProperty("line.separator");
fos.write(str);
❻ JAVA中 如何把字符串输入到一个TXT文件里,并从TXT里面读取出来输出到控制台
import java.io.*;
public class FileWrite {
public static void main(String args[]){
BufferedReader br=null;
BufferedWriter bw=null;
BufferedReader br1=null;
try {
br=new BufferedReader(new InputStreamReader(System.in));//字符输入流 从键盘读取数据
bw=new BufferedWriter(new FileWriter("D:/temp/title.txt"));//字符输出流 写数据到文件
br1=new BufferedReader(new FileReader("D:/temp/title.txt"));//字符输入流 从文件读取数据
String str=br.readLine();
bw.write(str);
bw.flush();
System.out.println(br1.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
bw.close();
br1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
❼ 怎么用java实现在一个txt文件中根据关键字查找信息并输出
个人愚见,还望指教
1、把txt文件封装成file对象;
2、如果是纯文字可以用FileReader如果不是的话可以用流转换一下FileInputStream;
3、维护一个数组将内容读取;
4、判断数组里面的关键词;(可以吧数组转换为String用contains方法)
❽ 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文件
输入无需使用字节流,直接字符流读取即可。
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();
}
}