首页编程createtempfile,createNewFile和createTempFile

createtempfile,createNewFile和createTempFile

编程之家2023-11-0670次浏览

亲爱的读者们,你是否对createtempfile和createNewFile和createTempFile的关系感到好奇?在本文中,我将深入探讨它们之间的联系,让你对此有更深刻的理解。

createtempfile,createNewFile和createTempFile

如何建立Temp这个文件夹

Temp文件夹的问题WINDOWS/Temp里的是Windows在正常的使用过程中产生的系统临时文件,可以把它们删除。不会影响系统的。在系统使用中有些文件没法删除也很正常的。Temp文件夹是什么文件夹?

C:\Documents and Settings用户里有个Local Settings文件夹,Local Settings里的Temp文件夹可以删除吗?Temp是什么文件夹?C盘里所有的TEMP文件夹都是临时文件夹文件夹可以删除为节省可用空间,你可以在启动电脑的时候自动清空Temp文件夹中的文件。方法是选择“开始|运行”,键入“sysedit”,单击“确定”,启动“系统配置编辑程序”,进入“c:\autoexec.bat”窗口,在文本末尾加入:deltree/y C:\Windows\Temp,保存并退出。此后,在你每次启动计算机后就会得到一个空白的Temp文件夹了。怎样更改TEMP文件夹的位置在非系统盘如D盘下新建文件夹Temp,然后右击“我的电脑”,选择“属性→高级→环境变量”,在弹出的“环境变量”窗口分别双击“用户变量”下的TEMP、TMP变量,把原来的“%USERPROFILE%\Local Settings\Temp”都修改为“D:\Temp”就ok了。

createNewFile和createTempFile

File.createNewFilefile类的createnewfile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败

File.createTempFile

的用途是你想要建立一个档案暂时使用,但是你不在乎其精确的档案名,只要不覆盖到已存在的档案时。可以制定

临时文件

createtempfile,createNewFile和createTempFile

的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的

临时文件夹

下。

JAVA里Flie类的creatnewfile与creattempfile有什么不同

后者的文件建立在默认的临时文件目录中不在当前目录

createNewFile

public boolean createNewFile()

createtempfile,createNewFile和createTempFile

throws IOException当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。检查文件是否存在,若不存在则创建该文件,这是单个操作,对于其他所有可能影响该文件的文件系统活动来说,该操作是不可分的。

createTempFile

public static File createTempFile(String prefix,

String suffix)

throws IOException在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。调用此方法等同于调用 createTempFile(prefix, suffix, null)。

参数:

prefix-用于生成文件名的前缀字符串;必须至少是三字符长

suffix-用于生成文件名的后缀字符串;可以为 null,在这种情况下,将使用后缀".tmp"

返回:

表示新建空文件的抽象路径名

java程序如何调用Graphviz

网上有别人已经写好的一个GraphViz.java类。java可以直接调用这个类实现画图功能,但是使用这个类的前提是你的电脑已经装了GraphViz软件,你可以在http://www.graphviz.org/Gallery.php下载windows版本,装完后,找到dot.exe所在的路径,我电脑上的是D:\Program Files\Graphviz2.30\bin\dot.exe,

将GraphViz.java中的这一路径改成你电脑上的路径,基本上就可以用了。

package Graphoutput;

// GraphViz.java- a simple API to call dot from Java programs

/*$Id$*/

/*

******************************************************************************

**

*(c) Copyright 2003 Laszlo Szathmary*

**

* This program is free software; you can redistribute it and/or modify it*

* under the terms of the GNU Lesser General Public License as published by*

* the Free Software Foundation; either version 2.1 of the License, or*

*(at your option) any later version.*

**

* This program is distributed in the hope that it will be useful, but*

* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY*

* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public*

* License for more details.*

**

* You should have received a copy of the GNU Lesser General Public License*

* along with this program; if not, write to the Free Software Foundation,*

* Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*

**

******************************************************************************

*/

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.InputStreamReader;

/**

*<dl>

*<dt>Purpose: GraphViz Java API

*<dd>

*

*<dt>Description:

*<dd> With this Java class you can simply call dot

* from your Java programs

*<dt>Example usage:

*<dd>

*<pre>

* GraphViz gv= new GraphViz();

* gv.addln(gv.start_graph());

* gv.addln("A-> B;");

* gv.addln("A-> C;");

* gv.addln(gv.end_graph());

* System.out.println(gv.getDotSource());

*

* String type="gif";

* File out= new File("out."+ type);// out.gif in this example

* gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type), out);

*</pre>

*</dd>

*

*</dl>

*

*@version v0.4, 2011/02/05(February)-- Patch of Keheliya Gallaba is added. Now you

* can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc.

*@version v0.3, 2010/11/29(November)-- Windows support+ ability

* to read the graph from a text file

*@version v0.2, 2010/07/22(July)-- bug fix

*@version v0.1, 2003/12/04(December)-- first release

*@author Laszlo Szathmary(<a rel="external nofollow" href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>)

*/

public class GraphViz

{

/**

* The dir. where temporary files will be created.

*/

//private static String TEMP_DIR="/tmp";// Linux

private static String TEMP_DIR="c:/temp";// Windows

/**

* Where is your dot program located? It will be called externally.

*/

// private static String DOT="/usr/bin/dot";// Linux

private static String DOT="D:\\Program Files\\Graphviz2.30\\bin\\dot.exe";// Windows

/**

* The source of the graph written in dot language.

*/

private StringBuilder graph= new StringBuilder();

/**

* Constructor: creates a new GraphViz object that will contain

* a graph.

*/

public GraphViz(){

}

/**

* Returns the graph's source description in dot language.

*@return Source of the graph in dot language.

*/

public String getDotSource(){

return graph.toString();

}

/**

* Adds a string to the graph's source(without newline).

*/

public void add(String line){

graph.append(line);

}

/**

* Adds a string to the graph's source(with newline).

*/

public void addln(String line){

graph.append(line+"\n");

}

/**

* Adds a newline to the graph's source.

*/

public void addln(){

graph.append('\n');

}

/**

* Returns the graph as an image in binary format.

*@param dot_source Source of the graph to be drawn.

*@param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png.

*@return A byte array containing the image of the graph.

*/

public byte[] getGraph(String dot_source, String type)

{

File dot;

byte[] img_stream= null;

try{

dot= writeDotSourceToFile(dot_source);

if(dot!= null)

{

img_stream= get_img_stream(dot, type);

if(dot.delete()== false)

System.err.println("Warning:"+ dot.getAbsolutePath()+" could not be deleted!");

return img_stream;

}

return null;

} catch(java.io.IOException ioe){ return null;}

}

/**

* Writes the graph's image in a file.

*@param img A byte array containing the image of the graph.

*@param file Name of the file to where we want to write.

*@return Success: 1, Failure:-1

*/

public int writeGraphToFile(byte[] img, String file)

{

File to= new File(file);

return writeGraphToFile(img, to);

}

/**

* Writes the graph's image in a file.

*@param img A byte array containing the image of the graph.

*@param to A File object to where we want to write.

*@return Success: 1, Failure:-1

*/

public int writeGraphToFile(byte[] img, File to)

{

try{

FileOutputStream fos= new FileOutputStream(to);

fos.write(img);

fos.close();

} catch(java.io.IOException ioe){ ioe.printStackTrace();return-1;}

return 1;

}

/**

* It will call the external dot program, and return the image in

* binary format.

*@param dot Source of the graph(in dot language).

*@param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png.

*@return The image of the graph in.gif format.

*/

private byte[] get_img_stream(File dot, String type)

{

File img;

byte[] img_stream= null;

try{

img= File.createTempFile("graph_","."+type, new File(GraphViz.TEMP_DIR));

Runtime rt= Runtime.getRuntime();

// patch by Mike Chenault

String[] args={DOT,"-T"+type, dot.getAbsolutePath(),"-o", img.getAbsolutePath()};

Process p= rt.exec(args);

p.waitFor();

FileInputStream in= new FileInputStream(img.getAbsolutePath());

img_stream= new byte[in.available()];

in.read(img_stream);

// Close it if we need to

if( in!= null) in.close();

if(img.delete()== false)

System.err.println("Warning:"+ img.getAbsolutePath()+" could not be deleted!");

}

catch(java.io.IOException ioe){

System.err.println("Error: in I/O processing of tempfile in dir"+ GraphViz.TEMP_DIR+"\n");

System.err.println(" or in calling external command");

ioe.printStackTrace();

}

catch(java.lang.InterruptedException ie){

System.err.println("Error: the execution of the external program was interrupted");

ie.printStackTrace();

}

return img_stream;}

/**

* Writes the source of the graph in a file, and returns the written file

* as a File object.

*@param str Source of the graph(in dot language).

*@return The file(as a File object) that contains the source of the graph.

*/

public File writeDotSourceToFile(String str) throws java.io.IOException

{

File temp;

try{

temp= File.createTempFile("graph_",".dot.tmp", new File(GraphViz.TEMP_DIR));

FileWriter fout= new FileWriter(temp);

fout.write(str);

fout.close();

}

catch(Exception e){

System.err.println("Error: I/O error while writing the dot source to temp file!");

return null;

}

return temp;

}

/**

* Returns a string that is used to start a graph.

*@return A string to open a graph.

*/

public String start_graph(){

return"digraph G{";

}

/**

* Returns a string that is used to end a graph.

*@return A string to close a graph.

*/

public String end_graph(){

return"}";

}

/**

* Read a DOT graph from a text file.

*

*@param input Input text file containing the DOT graph

* source.

*/

public void readSource(String input)

{

StringBuilder sb= new StringBuilder();

try

{

FileInputStream fis= new FileInputStream(input);

DataInputStream dis= new DataInputStream(fis);

BufferedReader br= new BufferedReader(new InputStreamReader(dis));

String line;

while((line= br.readLine())!= null){

sb.append(line);

}

dis.close();

}

catch(Exception e){

System.err.println("Error:"+ e.getMessage());

}

this.graph= sb;

}

}// end of class GraphViz

通过下面这个类调用graphViz.java

import java.io.File;

public class Proba

{

public static void main(String[] args)

{

Proba p= new Proba();

p.start();

// p.start2();

}

/**

* Construct a DOT graph in memory, convert it

* to image and store the image in the file system.

*/

private void start()

{

GraphViz gv= new GraphViz();

gv.addln(gv.start_graph());

gv.addln("A-> B;");

gv.addln("A-> C;");

gv.addln(gv.end_graph());

System.out.println(gv.getDotSource());

String type="gif";

// String type="dot";

// String type="fig";// open with xfig

// String type="pdf";

// String type="ps";

// String type="svg";// open with inkscape

// String type="png";

// String type="plain";

File out= new File("/tmp/out."+ type);// Linux

// File out= new File("c:/eclipse.ws/graphviz-java-api/out."+ type);// Windows

gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type), out);

}

/**

* Read the DOT source from a file,

* convert to image and store the image in the file system.

*/

private void start2()

{

// String dir="/home/jabba/eclipse2/laszlo.sajat/graphviz-java-api";// Linux

// String input= dir+"/sample/simple.dot";

String input="c:/eclipse.ws/graphviz-java-api/sample/simple.dot";// Windows

GraphViz gv= new GraphViz();

gv.readSource(input);

System.out.println(gv.getDotSource());

String type="gif";

// String type="dot";

// String type="fig";// open with xfig

// String type="pdf";

// String type="ps";

// String type="svg";// open with inkscape

// String type="png";

// String type="plain";

File out= new File("/tmp/simple."+ type);// Linux

// File out= new File("c:/eclipse.ws/graphviz-java-api/tmp/simple."+ type);// Windows

gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type), out);

}

}

关于createtempfile到此分享完毕,希望能帮助到您。

ssl证书如何安装,如何安装SSL证书指针数组 指针数组的数组名表示什么