
正文
java文件提取代码 java提取文件名
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
跪求Java中写入文件和从文件中读取数据的最佳的代码!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) {
String str = "123\r\n456";
writeFile(str);//写
String str1 = readFile();//读
System.out.println(str1);
}
/**
* 传递写的内容
* @param str
*/
static void writeFile(String str) {
try {
File file = new File("d:\\file.txt");
if(file.exists()){//存在
file.delete();//删除再建
file.createNewFile();
}else{
file.createNewFile();//不存在直接创建
}
FileWriter fw = new FileWriter(file);//文件写IO
fw.write(str);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 返回读取的内容
* @return
*/
static String readFile() {
String str = "", temp = null;
try {
File file = new File("d:\\file.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);//文件读IO
while((temp = br.readLine())!=null){//读到结束为止
str += (temp+"\n");
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
刚写的,够朋友好好学习一下啦,呵呵
多多看API,多多练习
相关问答
Q1: java中怎么提取java文件中的方法有哪些
如果想要获得当前文件中的文件名只需要String [] fileName = file.list();就可以了。如果要包括文件中的文件名就可以用递归的方式。下面是两个具体的实现。
其中public static String [] getFileName(String path)是只得到当前文件中的文件名。public static void getAllFileName(String path,ArrayListString fileName)是包括当前文件及其子文件的文件名。
public class GetFileName
{
public static String [] getFileName(String path)
{
File file = new File(path);
String [] fileName = file.list();
return fileName;
}
public static void getAllFileName(String path,ArrayListString fileName)
{
File file = new File(path);
File [] files = file.listFiles();
String [] names = file.list();
if(names != null)
fileName.addAll(Arrays.asList(names));
for(File a:files)
{
if(a.isDirectory())
{
getAllFileName(a.getAbsolutePath(),fileName);
}
}
}
public static void main(String[] args)
{
String [] fileName = getFileName("F:\\xiaoshuo");
for(String name:fileName)
{
System.out.println(name);
}
System.out.println("--------------------------------");
ArrayListString listFileName = new ArrayListString();
getAllFileName("F:\\xiaoshuo",listFileName);
for(String name:listFileName)
{
System.out.println(name);
}
}
}
运行时需要更改一下具体的文件夹。
Q2: linux下wsdl2java获取源码命令
运行命令WSDL2Java。
生成服务端代码命令是WSDL2Javauriwsdl文件全路径p包名dxmlbeanss,sssdssio生成的java代码存放路径。生成客户端包代码命令是WSDL2Java至uriwsdl文件全路径p包名dxmlbeansso生成的java代码存放路径。
源码就是指编写的最原始程序的代码。运行的软件是要经过编写的,程序员编写程序的过程中需要他们的语言。音乐家用五线谱和音符,建筑师用图纸和笔,那程序员的工作的语言就是源码了。
Q3: 一个文件夹下的多个txt文件,然后随机读取其中一个txt文件的内容(java代码)?
提供个思路:
1、把文件夹下所有txt文件java文件提取代码的文件名java文件提取代码,读取List里。
2、生成一个随机数,随机的范围是:0到List.size()-1。
3、用步骤2生产的随机数取个文件名。List.get(随机数变量)。
4、根据步骤3中取到的文件名,去读取文件内容。
这样就可以随机读取其中一个txt文件的内容了。
Q4: java读取文本文件代码
java读取文本文件java文件提取代码的方法有很多 这个例子主要介绍最简单 最常用java文件提取代码的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()];int i;for(i= ; i readLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249
关于java文件提取代码和java提取文件名的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。




