[英]Create text file dynamically in a specified directory, write data,read data from that file in java
Recently i have tried to write code of above given title and the problem is that i could not get sufficient date to write upon have a look at my code .it is showing some errors like 最近我尝试编写上面给定标题的代码,问题是我无法获得足够的日期来编写,请看一下我的代码。它显示了一些错误,例如
it is showing invalid token for semicolon after tagfile.createnewfile(); 在tagfile.createnewfile()之后显示分号的无效令牌; let us look at code:
让我们看一下代码:
public class WriteToFileExample { 公共类WriteToFileExample {
String path = "G:"+File.separator+"Software"+File.separator+"Files";
String fname= path+File.separator+"fileName.txt";
boolean bool=false;
File f = new File(path);
File f1 = new File(fname);
try {
f1.createNewFile();
bool=f.mkdirs() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static final String FILENAME = "G:\\Software\\Files\\anil.txt";
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String content = null;
String[] str = new String[100];
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
System.out.println(" enter no.of line to be inserted into file");
int k = sc.nextInt();
for (int i = 0; i <= k; i++) {
System.out.println(" enter " + i + " line data");
content = sc.nextLine();
bw.write(content);
str[i] = content;
}
System.out.println("The content present in\n
G:\\Software\\Files\\anil.txt is:"); G:\\ Software \\ Files \\ anil.txt是:“);
for (int i = 1; i <= k; i++)
System.out.println(str[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
} }
Please find the code and then you can use 请找到代码,然后就可以使用
**Writing** : PrintWriter,Scanner,BufferedWriter etc to write to that file
**Reading** :FileReader,BufferReader,Scanner with these readers etc
String path = "G:"+File.separator+"Software"+File.separator+"Files";
String fname= path+File.separator+"fileName.txt";
File f = new File(path);
File f1 = new File(fname);
f.mkdirs() ;
try {
f1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Ref : https://www.lynda.com/Java-tutorials/Reading-writing-text-files/107061/113497-4.html 参考: https : //www.lynda.com/Java-tutorials/Reading-writing-text-files/107061/113497-4.html
Refer above video which may help 请参考上面的视频,可能会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.