文件管理 · 2022年7月25日

java把字符串写进文件|java怎么把大字符串写入文件

① java怎么把大字符串写入文件

慢点应该没关系吧,可以考虑新建一个线程处理比较慢的业务,也就是说把写入字符串放到新建线程中执行,不影响主程序执行

② JAVA如何用流将字符串添加到文本文件中。

FileOutputStreampublic FileOutputStream(String name, boolean append) throws FileNotFoundException创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。 首先,如果有安全管理器,则用 name 作为参数调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。 参数:name – 与系统有关的文件名append – 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处

③ java怎么将字符串写入到文件

使用Java中的抄File类,url为文件的绝对地址,str为输入的字符串内容。代码如下图所示:String str="i love china!" File txt=new File("url"); if(!txt.exists()){ txt.createNewFile(); } byte bytes[]=new byte[512]; bytes=str.getBytes(); //新加的 int b=str.length(); //改 FileOutputStream fos=new FileOutputStream(txt); fos.write(bytes,0,b); fos.close();

④ java中如何把一段字符串写入文件中

使用Java中的File类,源url为文件的绝对地址,str为输入的字符串内容。代码如下图所示:String str="i love china!" File txt=new File("url"); if(!txt.exists()){ txt.createNewFile(); } byte bytes[]=new byte[512]; bytes=str.getBytes(); //新加的 int b=str.length(); //改 FileOutputStream fos=new FileOutputStream(txt); fos.write(bytes,0,b); fos.close();

⑤ 如何java向文件写入字符串

BufferedReader buff = new BufferedReader(new FileReader("abc.txt"));String str = "asdfsfas";for (int i = 0;i < str.length();i++) buff.write(str.charAt(i));buff.close();

⑥ java程序把字符串写入.java文件

就和写普通文本文件没区别

⑦ java中将字符串写入到文件中,但是文件中没有内容,急啊!求解

首先要确认是真的有内容的。。。。。File f=new File("b.txt"); if(!f.exists()){ f.createNewFile(); //多余的。。。。。 } bw 写完后,close()

⑧ Java中怎么将字符串按行写入到txt文件中

java写入文本文件的方法很多,比如FileWriter

FileWriterfw=newFileWriter("D:/Test.txt");Strings="helloworld
";fw.write(s,0,s.length());s="helloworld2
";fw.write(s,0,s.length());fw.flush();

这样就写了两行了。其中斜线n是换行符

⑨ java中如何将字符或者字符串写入到文件的指定位置

实现思路:就是重新定义一个文件,之后将之前的文件内容读取出来指定未指定的数据,之后再插入某些数据。可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。 BufferedReader bre = null;OutputStreamWriter pw = null;//定义一个流try {String file = "D:/test/test.xml";bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流pw = new OutputStreamWriter(new FileOutputStream(“D:/New.xml”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环{if(str.indexOf("<end>")){//判断如果满足xml的条件就插入字符串pw.write(str);//将要写入文件的内容,可以多次writepw.write(“插入的内容”);//将要写入文件的内容,可以多次write}};bre.close();pw.close();//关闭流备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

⑩ 在Java中怎样把StringBuffer中的字符串写入到文件

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Characters are copied from this sequence into the destination character array dst.得到 char[],就可以写到文件中专了 。。属。。。。。。。。。。。。