向文件添加动态名称
Add a dynamic name to file
我做了一个创建txt文件的按钮,这里一切正常。
但问题是当我创建第一个:TXT 文件时,我不知道我需要什么或需要做什么才能继续动态创建 TXT 文件。
这是我的代码:
int c;
c = 0;
c++;
String Name = "TXT" + c +".txt";
File TXT = new File(Name);
TXT.createNewFile();
这创建一个:TXT1.txt
但不创建 TXT2.txt、TXT3.txt 等。
我想动态增加数字。感谢阅读。
将其包裹在一个 for 循环中,并像这样排除计数器:
for(int i = 1; i < yourMaximumRun; ++i)
{
String Name = "TXT" + i +".txt";
File TXT = new File(Name);
try
{
TXT.createNewFile();
}
}
如果这是您方法中的代码,c 总是从 0 开始并递增到 1。
如果您再次按下按钮,c 将再次从 0 开始并递增。您需要将 c 保存在方法之外的某个地方,而不是每次都重新初始化它。
1) Inside vs Outside:
If you declare your object inside a method, it will be visible only in
this method. Basically, if you put brackets around it, it's only
visible/accessible from within these brackets.
If you declare your object outside the method (inside the class), it
depends on the access modifier. By default, it's visible/accessible
from within that class and the whole package.
2) Static
Static means, that this Object/Variable belongs to the class itself,
and not to its objects
source
为了模拟按钮的点击,我做了这个小代码:
package main.application;
import java.io.File;
import java.io.IOException;
public class Main {
private static int incrementFileName = 1;
private static final String PATH = "C:\Users\user\Desktop\";
public static void main(String[] args) throws IOException {
//Each time the button is pressed.
for (int c = 0; c < 5; c++)
{
incrementFileName++;
buttonClicked();
}
}
private static void buttonClicked() throws IOException
{
String Name = "TXT" + incrementFileName +".txt";
File TXT = new File(PATH + Name);
TXT.createNewFile();
}
}
如您所见,您需要在用于创建 new File
的方法之外声明 incrementFileName
(您的 c
),并在每次按下按钮时递增它被按下。
我做了一个创建txt文件的按钮,这里一切正常。
但问题是当我创建第一个:TXT 文件时,我不知道我需要什么或需要做什么才能继续动态创建 TXT 文件。
这是我的代码:
int c;
c = 0;
c++;
String Name = "TXT" + c +".txt";
File TXT = new File(Name);
TXT.createNewFile();
这创建一个:TXT1.txt 但不创建 TXT2.txt、TXT3.txt 等。
我想动态增加数字。感谢阅读。
将其包裹在一个 for 循环中,并像这样排除计数器:
for(int i = 1; i < yourMaximumRun; ++i)
{
String Name = "TXT" + i +".txt";
File TXT = new File(Name);
try
{
TXT.createNewFile();
}
}
如果这是您方法中的代码,c 总是从 0 开始并递增到 1。
如果您再次按下按钮,c 将再次从 0 开始并递增。您需要将 c 保存在方法之外的某个地方,而不是每次都重新初始化它。
1) Inside vs Outside:
If you declare your object inside a method, it will be visible only in this method. Basically, if you put brackets around it, it's only visible/accessible from within these brackets.
If you declare your object outside the method (inside the class), it depends on the access modifier. By default, it's visible/accessible from within that class and the whole package.
2) Static
Static means, that this Object/Variable belongs to the class itself, and not to its objects
source
为了模拟按钮的点击,我做了这个小代码:
package main.application;
import java.io.File;
import java.io.IOException;
public class Main {
private static int incrementFileName = 1;
private static final String PATH = "C:\Users\user\Desktop\";
public static void main(String[] args) throws IOException {
//Each time the button is pressed.
for (int c = 0; c < 5; c++)
{
incrementFileName++;
buttonClicked();
}
}
private static void buttonClicked() throws IOException
{
String Name = "TXT" + incrementFileName +".txt";
File TXT = new File(PATH + Name);
TXT.createNewFile();
}
}
如您所见,您需要在用于创建 new File
的方法之外声明 incrementFileName
(您的 c
),并在每次按下按钮时递增它被按下。