實(shí)現(xiàn)代碼如下:
上城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信: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對(duì)象
HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet對(duì)象
// 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);//設(shè)置cell的整數(shù)類型的值
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);//設(shè)置cell浮點(diǎn)類型的值
row.createCell((short)2).setCellValue("test");//設(shè)置cell字符類型的值
row.createCell((short)3).setCellValue(true);//設(shè)置cell布爾類型的值
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell樣式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//設(shè)置cell樣式為定制的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());//設(shè)置cell為日期類型的值
dCell.setCellStyle(cellStyle); //設(shè)置該cell日期的顯示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//設(shè)置cell編碼解決中文高位字節(jié)截?cái)?/p>
csCell.setCellValue("中文測(cè)試_Chinese Words Test");//設(shè)置中西文結(jié)合字符串
row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立錯(cuò)誤cell
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
Java是由Sun Microsystems公司推出的Java面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言(以下簡(jiǎn)稱Java語(yǔ)言)和Java平臺(tái)的總稱。由James Gosling和同事們共同研發(fā),并在1995年正式推出。Java最初被稱為Oak,是1991年為消費(fèi)類電子產(chǎn)品的嵌入式芯片而設(shè)計(jì)的。1995年更名為Java,并重新設(shè)計(jì)用于開發(fā)Internet應(yīng)用程序。
用Java實(shí)現(xiàn)的HotJava瀏覽器(支持Java applet)顯示了Java的魅力:跨平臺(tái)、動(dòng)態(tài)Web、Internet計(jì)算。從此,Java被廣泛接受并推動(dòng)了Web的迅速發(fā)展,常用的瀏覽器均支持Javaapplet。另一方面,Java技術(shù)也不斷更新。Java自面世后就非常流行,發(fā)展迅速,對(duì)C++語(yǔ)言形成有力沖擊。在全球云計(jì)算和移動(dòng)互聯(lián)網(wǎng)的產(chǎn)業(yè)環(huán)境下,Java更具備了顯著優(yōu)勢(shì)和廣闊前景。2010年Oracle公司收購(gòu)Sun Microsystems。
package IO; // 定義包名
import java.io.*;// 引入java.io包下的所有類
// 定義一個(gè)類
public class FileExample {
// 定義構(gòu)造函數(shù)
public FileExample() {
// 調(diào)用父類的構(gòu)造函數(shù)
super();
}
// 定義主方法
public static void main(String[] args) {
// 捕獲異常
try {
// 定義了一個(gè)變量, 用于標(biāo)識(shí)輸出文件
String outfile = "demoout.xml";
// 定義了一個(gè)變量, 用于標(biāo)識(shí)輸入文件
String infile = "demoin.xml";
/**
* 用FileOutputStream定義一個(gè)輸入流文件,然后用BuferedOutputStream調(diào)用FileOutputStream對(duì)象生成一個(gè)緩沖輸出流
* 然后用DataOutputStream調(diào)用BuferedOutputStream對(duì)象生成數(shù)據(jù)格式化輸出流
*/
DataOutputStream dt=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));
BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(dt, "GBK"));
// 對(duì)中文的處理
/**
*用FileInputStream定義一個(gè)輸入流文件,然后用BuferedInputStream調(diào)用FileInputStream對(duì)象生成一個(gè)緩沖輸出流
* ,其后用DataInputStream中調(diào)用BuferedInputStream對(duì)象生成數(shù)據(jù)格式化輸出流
*/
DataInputStream rafFile1 = new DataInputStream(new BufferedInputStream(new FileInputStream(infile)));
BufferedReader rafFile = new BufferedReader(new InputStreamReader(rafFile1, "GBK"));
String xmlcontent = "";
char tag = 0;// 文件友字符0結(jié)束
while (tag != (char) (-1)) {
xmlcontent = xmlcontent + tag + rafFile.readLine() + '\n';
tag = (char) rafFile.read();
}
NewFile.write(xmlcontent);// 將內(nèi)容寫入到文件中
NewFile.flush();//清空緩沖區(qū)使輸出流寫出
NewFile.close(); // 關(guān)閉流
rafFile.close();// 關(guān)閉流
System.gc();// 調(diào)用垃圾回收器
} catch (NullPointerException exc) {
// 如果發(fā)生空指針異常則走這里
exc.printStackTrace();
} catch (java.lang.IndexOutOfBoundsException outb) {
// 如果發(fā)生索引越界則執(zhí)行這里的方法
System.out.println(outb.getMessage());
outb.printStackTrace();//控制臺(tái)的紅字
} catch (FileNotFoundException fex) {
// 這里用于處理文件未找到異常
System.out.println("fex" + fex.getMessage());
} catch (IOException iex) {
// 這里是IO異常
System.out.println("iex" + iex.getMessage());
}
}
}
常用的輸入語(yǔ)句是:
輸入字符串:new Scanner(System.in).next();
輸入整數(shù):new Scanner(System.in).nextInt();
輸入小數(shù):new Scanner(System.in).nextDouble();
常用的輸出語(yǔ)句:
換行輸出: System.out.println(變量或字符串);
非換行輸出: System.out.print(變量或字符串);
換行輸出錯(cuò)誤提示(默認(rèn)是紅字):System.err.println(變量或字符串);
不換行輸出錯(cuò)誤提示(默認(rèn)是紅字): System.err.print(變量或字符串));
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");//注意文件路徑正確,這里測(cè)試放在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的API中提供了用于對(duì)象輸入輸出文件的操作,實(shí)例代碼如下:
定義單詞類如下(注意:你定義的類要實(shí)現(xiàn)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. 對(duì)象輸入輸出api測(cè)試類
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;
}
}
把兩段代碼拷貝到一個(gè)包下即可運(yùn)行了,希望您的問(wèn)題得到解答
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+"????標(biāo)題:"+name+"????內(nèi)容:"+content;
}
}
public?class?Test?{
public?static?void?main(String[]?args)?{
Book?book?=?new?Book();
Scanner?scanner?=?new?Scanner(System.in);
System.out.print("請(qǐng)輸入作者:");
book.setAuthor(scanner.next());
System.out.print("請(qǐng)輸入書名:");
book.setName(scanner.next());
System.out.print("請(qǐng)輸入內(nèi)容:");
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輸入和輸出流
網(wǎng)站鏈接:http://chinadenli.net/article38/doddgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、域名注冊(cè)、微信公眾號(hào)、Google、微信小程序、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)