欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

關(guān)于java代碼txt的信息

Java如何讀寫txt文件的代碼

有關(guān)Java如何讀寫txt文件這個(gè)問題經(jīng)常在面試時(shí)會(huì)被問到,不懂或不熟悉的同志們可是要記好了喲!先來看下具體實(shí)現(xiàn)吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 讀取數(shù)據(jù) */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 寫入數(shù)據(jù) */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }

為肥城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及肥城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、做網(wǎng)站、肥城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

java怎樣實(shí)現(xiàn)讀寫TXT文件

主要有用到j(luò)ava原生態(tài)的Io類,沒有第三個(gè)包。直接上代碼:

import?java.io.*;

public?class?write?{

public?static?void?main(String[]?args)?{

write("E://123.txt",?"hello");

}

public?static?void?write(String?path,?String?content)?{

try?{

File?f?=?new?File(path);

if?(f.exists())?{

System.out.println("文件存在");

}?else?{

System.out.println("文件不存在,正在創(chuàng)建...");

if?(f.createNewFile())?{

System.out.println("文件創(chuàng)建成功!");

}?else?{

System.out.println("文件創(chuàng)建失敗!");

}

}

BufferedWriter?output?=?new?BufferedWriter(new?FileWriter(f));

output.write(content);

output.close();

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

}

用java代碼把txt文檔中資料導(dǎo)入到數(shù)據(jù)庫

BufferedReader input;

try {

String s = new String();

input = new BufferedReader(new FileReader("f:\\123.txt"));

while ((s = input.readLine()) != null) { // 判斷是否讀到了最后一行

String info[] = s.split(" ");

System.out.println( info[0] + " " + info[1] + " " + info[2] );

}

input.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

把info[0] + " " + info[1] + " " + info[2] 這三個(gè)值放在insert語句里就行了 經(jīng)過測(cè)試

java代碼 如何向TXT文件寫入內(nèi)容?

向txt文件寫入內(nèi)容基本思路就是獲得一個(gè)file對(duì)象,新建一個(gè)txt文件,打開I/O操作流,使用寫入方法進(jìn)行讀寫內(nèi)容,示例如下:

package?common;

import?java.io.*;

import?java.util.ArrayList;

public?class?IOTest?{

public?static?void?main?(String?args[])?{

ReadDate();

WriteDate();

}

/**

*?讀取數(shù)據(jù)

*/

public?static?void?ReadDate()?{

String?url?=?“e:/2.txt”;

try?{

FileReader?read?=?new?FileReader(new?File(url));

StringBuffer?sb?=?new?StringBuffer();

char?ch[]?=?new?char[1024];

int?d?=?read.read(ch);

while(d!=-1){

String?str?=?new?String(ch,0,d);

sb.append(str);

d?=?read.read(ch);

}

System.out.print(sb.toString());

}?catch?(FileNotFoundException?e)?{

e.printStackTrace();

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

/**

*?寫入數(shù)據(jù)

*/

public?static?void?WriteDate()?{

try{

File?file?=?new?File(“D:/abc.txt”);

if?(file.exists())?{

file.delete();

}

file.createNewFile();

BufferedWriter?output?=?new?BufferedWriter(new?FileWriter(file));

ArrayList?ResolveList?=?new?ArrayList();

for?(int?i?=?0;?i??10;?i++)?{

ResolveList.add(Math.random()*?100);

}

for?(int?i=0?;i?

output.write(String.valueOf(ResolveList.get(i))?+?“\n”);

}

output.close();

}?catch?(Exception?ex)?{

System.out.println(ex);

}

}

}

原文出自【比特網(wǎng)】,轉(zhuǎn)載請(qǐng)保留原文鏈接:

怎樣用java代碼獲取txt文本的指定值

[Java] view plain copy

import java.io.*;

public class hh {

/**

* @param args

*/

public static void main(String[] args) {

// 指定讀取的行號(hào)

int lineNumber = 2;

// 讀取文件

//File sourceFile = new File("D:/java/test.txt");

File sourceFile = new File("C://TEXT.txt");

try {

// 讀取指定的行

readAppointedLineNumber(sourceFile, lineNumber);

// 獲取文件的內(nèi)容的總行數(shù)

System.out.println(getTotalLines(sourceFile));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 讀取文件指定行。

static void readAppointedLineNumber(File sourceFile, int lineNumber)

throws IOException {

FileReader in = new FileReader(sourceFile);

LineNumberReader reader = new LineNumberReader(in);

String s = "";

if (lineNumber = 0 || lineNumber getTotalLines(sourceFile)) {

System.out.println("不在文件的行數(shù)范圍(1至總行數(shù))之內(nèi)。");

System.exit(0);

}

int lines = 0;

while (s != null) {

lines++;

s = reader.readLine();

if((lines - lineNumber) == 0) {

System.out.println(s);

System.exit(0);

}

}

reader.close();

in.close();

}

// 文件內(nèi)容的總行數(shù)。

static int getTotalLines(File file) throws IOException {

FileReader in = new FileReader(file);

LineNumberReader reader = new LineNumberReader(in);

String s = reader.readLine();

int lines = 0;

while (s != null) {

lines++;

s = reader.readLine();

if(lines=2){

if(s!=null){

System.out.println(s+"$");

}

}

}

reader.close();

in.close();

return lines;

}

}

怎么用java代碼打開txt文件?

import java.awt.*; import java.io.*; import java.awt.event.*; import javax.swing.*; public class Test { public Test() { JFrame f=new JFrame("TEST"); Container c=f.getContentPane(); c.setLayout(new FlowLayout()); JButton b=new JButton("幫助"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String cmd="rundll32 url.dll FileProtocolHandler ";//啟動(dòng)相應(yīng)的windows程序來打開文件 Process p = Runtime.getRuntime().exec(cmd); } catch (Exception e1) { System.out.println(e1); } } }); c.add(b); f.setBounds(100,100,300,300); f.setVisible(true); } public static void main(String arg[]) { new Test(); } } //Rundll32.exe DLLname,Functionname [Arguments] ,DLLname為需要執(zhí)行的DLL文件名; //Functionname為前邊需要執(zhí)行的DLL文件的具體引出函數(shù);[Arguments]為引出函數(shù)的具體參數(shù)

本文標(biāo)題:關(guān)于java代碼txt的信息
當(dāng)前路徑:http://chinadenli.net/article11/dsggsdd.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT商城網(wǎng)站外貿(mào)網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站域名注冊(cè)App設(shè)計(jì)

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司