java写入文件到指定文件夹的方法主要有两种:利用PrintStream和利用StringBuffer
例如将文本“I'm the text to be write”写入到文件夹D:/test下,并命名为test.txt,则两种方式简单实现代码如下:
1. 利用PrintStream写文件
publicvoidPrintStreamDemo(){
try{
FileOutputStreamout=newFileOutputStream("D:/test.txt");
PrintStreamp=newPrintStream(out);
p.println("I'mthetexttobewrite");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}
2. 利用StringBuffer写文件
publicvoidStringBufferDemo()throwsIOException{
Filefile=newFile("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStreamout=newFileOutputStream(file,true);
StringBuffersb=newStringBuffer();
sb.append("I'mthetexttobewrite");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}
提示:利用StringBuffer写文件可以设定使用何种编码,有效解决中文问题。
『贰』 java中获取文件路径的几种方式
获取当前类的所在工程路径;如果未添加“/”,则代码如下:
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
执行结果为:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;第二种方法为:
File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
执行结果为:C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;第三种方法为:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
执行结果为:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径;第四种方法为:
System.out.println(System.getProperty("user.dir"));
执行结果为:C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径;第五种方法为:
System.out.println(System.getProperty("java.class.path"));
执行结果为:C:\Documents and Settings\Administrator\workspace\projectName\bin
以上介绍了五种获取文件路径的方法,每种方法都有其特点和适用场景。第一种方法适用于需要获取类所在目录的路径,但结果包含bin文件夹;第二种方法适用于获取文件系统中的绝对路径;第三种方法适用于获取类加载器资源的URL路径,结果包含文件协议;第四种方法获取当前工作目录,即工程根目录;第五种方法获取类路径,通常指向编译后的类文件所在的目录。
在实际开发中,根据具体需求选择合适的方法。例如,如果需要获取源代码文件的路径,可以使用第三种方法;如果需要获取编译后的类文件路径,则使用第五种方法更为合适。
需要注意的是,路径格式在Windows和Linux系统中可能存在差异,因此在跨平台项目中应谨慎使用这些方法。同时,建议在编写代码时考虑路径的可读性和安全性,避免硬编码路径。
在处理文件路径时,务必考虑文件系统的限制和特殊字符,确保路径的正确性和兼容性。此外,对于敏感文件和目录,应采取适当的访问控制措施,以防止意外访问或修改。
『叁』 java文件读写,在一个已经有内容的文件中,追加第一行,如何做到
我的想法是可以用RandomAccessFile,这个类的seek方法,想在文件的哪个位置插入内容都行。所以你的第一行就不在话下了。但是,这个会覆盖你文件中插入位置后面的内容。相当于我们在输入的时候,按了键盘的insert键盘。所以,像你这种情况只能用临时文件来存储原有的内容,然后把要插入的数据写入文件,再把临时文件的内容追加到文件中。x0dx0avoid insert(String filename,int pos,String insertContent){//pos是插入的位置x0dx0a File tmp = File.createTempFile("tmp",null);x0dx0a tmp.deleteOnExit();x0dx0a try{x0dx0a RandomAccessFile raf = new RandomAccessFile(filename,"rw");x0dx0a FileOutputStream tmpOut = new FileOutputStream(tmp);x0dx0a FileInputStream tmpIn = new FileInputStream(tmp);x0dx0a raf.seek(pos);//首先的话是0x0dx0a byte[] buf = new byte[64];x0dx0a int hasRead = 0;x0dx0a while((hasRead = raf.read(buf))>0){x0dx0a //把原有内容读入临时文件x0dx0a tmpOut.write(buf,0,hasRead);x0dx0a x0dx0a }x0dx0a raf.seek(pos);x0dx0a raf.write(insertContent.getBytes());x0dx0a //追加临时文件的内容x0dx0a while((hasRead = tmpIn.read(buf))>0){x0dx0a raf.write(buf,0,hasRead);x0dx0a }x0dx0a }x0dx0a}