java输入输出流代码 java输入和输出流

java中输入输出流如何把数据输出为Excel表格形式

实现代码如下:

上城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

publicclass CreateCells

{

publicstaticvoid main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象

// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellValue(1);//设置cell的整数类型的值

// Or do it on one line.

row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellValue("test");//设置cell字符类型的值

row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellValue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

Java是由Sun Microsystems公司推出的Java面向对象程序设计语言(以下简称Java语言)和Java平台的总称。由James Gosling和同事们共同研发,并在1995年正式推出。Java最初被称为Oak,是1991年为消费类电子产品的嵌入式芯片而设计的。1995年更名为Java,并重新设计用于开发Internet应用程序。

用Java实现的HotJava浏览器(支持Java applet)显示了Java的魅力:跨平台、动态Web、Internet计算。从此,Java被广泛接受并推动了Web的迅速发展,常用的浏览器均支持Javaapplet。另一方面,Java技术也不断更新。Java自面世后就非常流行,发展迅速,对C++语言形成有力冲击。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。2010年Oracle公司收购Sun Microsystems。

java 输入输出流

package IO; // 定义包名

import java.io.*;// 引入java.io包下的所有类

// 定义一个类

public class FileExample {

// 定义构造函数

public FileExample() {

// 调用父类的构造函数

super();

}

// 定义主方法

public static void main(String[] args) {

// 捕获异常

try {

// 定义了一个变量, 用于标识输出文件

String outfile = "demoout.xml";

// 定义了一个变量, 用于标识输入文件

String infile = "demoin.xml";

/**

* 用FileOutputStream定义一个输入流文件,然后用BuferedOutputStream调用FileOutputStream对象生成一个缓冲输出流

* 然后用DataOutputStream调用BuferedOutputStream对象生成数据格式化输出流

*/

DataOutputStream dt=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));

BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(dt, "GBK"));

// 对中文的处理

/**

*用FileInputStream定义一个输入流文件,然后用BuferedInputStream调用FileInputStream对象生成一个缓冲输出流

* ,其后用DataInputStream中调用BuferedInputStream对象生成数据格式化输出流

*/

DataInputStream rafFile1 = new DataInputStream(new BufferedInputStream(new FileInputStream(infile)));

BufferedReader rafFile = new BufferedReader(new InputStreamReader(rafFile1, "GBK"));

String xmlcontent = "";

char tag = 0;// 文件友字符0结束

while (tag != (char) (-1)) {

xmlcontent = xmlcontent + tag + rafFile.readLine() + '\n';

tag = (char) rafFile.read();

}

NewFile.write(xmlcontent);// 将内容写入到文件中

NewFile.flush();//清空缓冲区使输出流写出

NewFile.close(); // 关闭流

rafFile.close();// 关闭流

System.gc();// 调用垃圾回收器

} catch (NullPointerException exc) {

// 如果发生空指针异常则走这里

exc.printStackTrace();

} catch (java.lang.IndexOutOfBoundsException outb) {

// 如果发生索引越界则执行这里的方法

System.out.println(outb.getMessage());

outb.printStackTrace();//控制台的红字

} catch (FileNotFoundException fex) {

// 这里用于处理文件未找到异常

System.out.println("fex" + fex.getMessage());

} catch (IOException iex) {

// 这里是IO异常

System.out.println("iex" + iex.getMessage());

}

}

}

Java的常用输入输出语句?

常用的输入语句是:

输入字符串:new Scanner(System.in).next();

输入整数:new Scanner(System.in).nextInt();

输入小数:new Scanner(System.in).nextDouble();

常用的输出语句:

换行输出: System.out.println(变量或字符串);

非换行输出: System.out.print(变量或字符串);

换行输出错误提示(默认是红字):System.err.println(变量或字符串);

不换行输出错误提示(默认是红字): System.err.print(变量或字符串));

java的输入输出流

package A;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class MyFileReader {

public static void main(String[] args) throws IOException {

File sf = new File("D:/IODemo1.txt");//注意文件路径正确,这里测试放在D盘了

File df = new File("D:/IODemo2.txt");

MyFileReader.copy(sf, df);

}

public static void copy(File srcfile, File desfile) {

try {

InputStream is = new FileInputStream(srcfile);

OutputStream os = new FileOutputStream(desfile);

int c;

while ((c = is.read()) != -1) {

os.write(c);

}

is.close();

os.flush();

os.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

System.out.println("没有找到文件");

} catch (IOException e) {

e.printStackTrace();

System.out.println("文件输入输出异常");

}

}

}

java输入输出流与文件,求源代码!感谢大佬!

你好,java的API中提供了用于对象输入输出文件的操作,实例代码如下:

定义单词类如下(注意:你定义的类要实现Serializable接口)

public class Words implements Serializable {

private int size;

private String[] words;

public Words(){};

public Words(String...strs){

this.words = strs;

this.size = strs.length;

}

@Override

public String toString() {

return "Words{" +

"size=" + size +

", words=" + Arrays.toString(words) +

'}';

}

}

2. 对象输入输出api测试类

public class ObjIOTest {

public static void main(String[] args) {

String path = "d:/myIOTest.txt";

ObjIOTest objIOTest = new ObjIOTest();

Words words = new Words("hello", "my", "dear", "friend");

try {

objIOTest.writeObject(path,words);

Words wordsFromFile = (Words)objIOTest.readObject(path);

System.out.println(wordsFromFile.toString());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

//java serialize a object to file

public void writeObject(String path,Object map) throws IOException{

File f=new File(path);

FileOutputStream out=new FileOutputStream(f);

ObjectOutputStream objwrite=new ObjectOutputStream(out);

objwrite.writeObject(map);

objwrite.flush();

objwrite.close();

}

// read the object from the file

public Object readObject(String path) throws IOException, ClassNotFoundException{

FileInputStream in=new FileInputStream(path);

ObjectInputStream objread=new ObjectInputStream(in);

Object map=objread.readObject();

objread.close();

return map;

}

}

把两段代码拷贝到一个包下即可运行了,希望您的问题得到解答

java 输入/输出流

public class Book {

private String name;

private String author;

private String content;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

@Override

public String toString() {

return "作者:"+author+"    标题:"+name+"    内容:"+content;

}

}

public class Test {

public static void main(String[] args) {

Book book = new Book();

Scanner scanner = new Scanner(System.in);

System.out.print("请输入作者:");

book.setAuthor(scanner.next());

System.out.print("请输入书名:");

book.setName(scanner.next());

System.out.print("请输入内容:");

book.setContent(scanner.next());

scanner.close();

try {

FileWriter writer = new FileWriter("1.txt", true);

writer.write(book.toString());

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

代码如上所示

名称栏目:java输入输出流代码 java输入和输出流
网站链接:https://www.cdcxhl.com/article38/doddgsp.html

成都网站建设公司_创新互联,为您提供ChatGPT域名注册微信公众号Google微信小程序企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站