关于java代码txt的信息
Java如何读写txt文件的代码
有关Java如何读写txt文件这个问题经常在面试时会被问到,不懂或不熟悉的同志们可是要记好了哟!先来看下具体实现吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ 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(); } } /** * 写入数据 */ 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); } } }
为肥城等地区用户提供了全套网页设计制作服务,及肥城网站建设行业解决方案。主营业务为成都网站建设、做网站、肥城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
java怎样实现读写TXT文件
主要有用到java原生态的Io类,没有第三个包。直接上代码:
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("文件不存在,正在创建...");
if (f.createNewFile()) {
System.out.println("文件创建成功!");
} else {
System.out.println("文件创建失败!");
}
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(content);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
用java代码把txt文档中资料导入到数据库
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] 这三个值放在insert语句里就行了 经过测试
java代码 如何向TXT文件写入内容?
向txt文件写入内容基本思路就是获得一个file对象,新建一个txt文件,打开I/O操作流,使用写入方法进行读写内容,示例如下:
package common;
import java.io.*;
import java.util.ArrayList;
public class IOTest {
public static void main (String args[]) {
ReadDate();
WriteDate();
}
/**
* 读取数据
*/
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();
}
}
/**
* 写入数据
*/
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);
}
}
}
原文出自【比特网】,转载请保留原文链接:
怎样用java代码获取txt文本的指定值
[Java] view plain copy
import java.io.*;
public class hh {
/**
* @param args
*/
public static void main(String[] args) {
// 指定读取的行号
int lineNumber = 2;
// 读取文件
//File sourceFile = new File("D:/java/test.txt");
File sourceFile = new File("C://TEXT.txt");
try {
// 读取指定的行
readAppointedLineNumber(sourceFile, lineNumber);
// 获取文件的内容的总行数
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("不在文件的行数范围(1至总行数)之内。");
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();
}
// 文件内容的总行数。
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 ";//启动相应的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为需要执行的DLL文件名; //Functionname为前边需要执行的DLL文件的具体引出函数;[Arguments]为引出函数的具体参数
本文标题:关于java代码txt的信息
分享URL:http://hbruida.cn/article/dsggsdd.html