「java导出指定文件」java导出可执行文件
本篇文章给大家谈谈java导出指定文件,以及java导出可执行文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java如何中将答案输出到指定文件中
File file = new File("指定文件路径");
FileOutputStream fos = new FileOutputStream(file);
fos.write("内容");
写不同的内容要用不同的流,可以用特殊的过滤流套接在文件流上来实现。看看JAVA的io包就明白了
java 导出文件命名
//Rename.java
import java.io.File;
public class Rename {
public static void main(String[] args) {
//这是Linux系统下的目录结构,如果你的是Windows系统,请适当更改(可以任意指定)
File directory = new File("/home/lsx/temp");
String suffix = ".txt";
//自动产生11个文件,名称序号自动递增
for(int i=0; i11; i++){
//取得一个File对象的引用
File f = createFile(directory,suffix);
if(f!=null){
//用了这个文件的引用,就可以做你想做了事了~~
}
else
System.out.println(err);
}
}
//初始前缀
private static final String prefix = "001";
//错误消息
private static String err = null;
//创建一个新文件,如果有同名文件存在,根据规则会自动重命名.
public static File createFile(File dir,String suffix){
//如果目录不存在,自动创建
if(!dir.exists())
dir.mkdirs();
//检查dir是否是一个目录
else if(dir.isFile()){
err = "指定的目录是一个文件。";
return null;
}
//检查目录是否可写
else if(!dir.canWrite()){
err="指定的目录不能被写入数据。";
return null;
}
//创建文件对象
File tmp = new File(dir,prefix+suffix);
//如果文件已存在则自动更名
while(tmp.exists()){
try{
String pre = tmp.getName().substring(0,tmp.getName().indexOf(suffix));
int prei = Integer.parseInt(pre)+1;
pre = String.valueOf(prei);
while(pre.length()prefix.length())pre="0"+pre;
tmp = new File(dir,pre+suffix);
}catch(Exception e){
err = "未知错误。"+
"\nError message:"+e.getMessage()+
"\nStack trace:"+e.getStackTrace().toString();
return null;
}
}
try {
tmp.createNewFile();
} catch (Exception e) {
err = "创建新文件失败。";
return null;
}
return tmp;
}
}
如何将java程序输出的结果存到指定文件
File file = new File("D:/test.txt");
FileOutputStream fos = new FileOutputStream(file);
String content = "这里是要输入的数据";
fos.write(content.getBytes());
fos.flush();
fos.close();
java中怎么将内容输出到指定txt文件中
public static boolean writefile(String path,String content){
try{
OutputStream out = new FileOutputStream(path);
out.write(content.getBytes());
out.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
java导出指定文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java导出可执行文件、java导出指定文件的信息别忘了在本站进行查找喔。