我还是java新手,只有一个学期。我有第一次实习,这不是一个编程实习,只是一般的IT实习,因为这只是我的第一个学期。 我的老板不懂Java,大楼里也没有人知道。他知道我有一些基本的编程经验,并告诉我要解决他遇到的问题。他有一个保存的报告和最后一行,报告的最后一个字符是字符转弯符号,我们需要删除它,因为它在网站上给我们带来了问题。 我不确定我是否在正确的轨道上,此时我只是在试错。请帮忙:D
public class RemoveChar {
public static void main(String[] args) throws FileNotFoundException {
// Variables and stuff
Scanner keyScan = new Scanner(System.in);
JFrame frameOne = new JFrame ("File Name");
Scanner fileScan = new Scanner(System.in);
String fileName;
// Ask user for file name
System.out.print("What is the file full file name? ");
fileName = fileScan.nextLine();
// Add .txt if the user forgets to put it in the prompt
if (!fileName.contains(".txt"))
fileName += ".txt";
//Test to see if file exists
File myFile = new File(fileName);
if(!myFile.exists()){
System.out.println(fileName + " does not exist. ");
System.exit(0);
}
fWriter = new FileWriter("config/lastWindow.txt", true);
/*while(fileName.hasNext()){
}
File
BufferedReader inputFile = new BufferedReader(new FileReader("C:\\" + fileScan));
//Scanner reader = new Scanner (inputFile);
*/
}
}
答案 0 :(得分:2)
文件有多大?如果它们不是那么大,你可以将整个文件读成一个字符串然后砍掉最后一个字符:
//Set delimiter to end-of-string anchor \Z so that you can read the
//file in with just one call to next()
//from: http://stackoverflow.com/a/3403112/263004
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
String withoutLastCharacter = content.substring(0, content.length - 1);
然后你只需要将withoutLastCharacter
写到文件中。
否则,您需要逐行读取原始文件并将其写入临时文件,然后将该文件复制到原始文件上。但是,如果你在最后一行,你将砍掉最后一个字符。这里有一些代码可以让您了解基本逻辑:
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
//If this is the last line chop off the last character.
if(!scanner.hasNextLine()) {
line = line.substring(0, line.length - 1);
}
//Write line out to temporary file
...
}
您还提到它不一定是Java。如果您使用的是Linux或Mac,则可以使用sed
:
sed -i '$s/.$//' <filename>
这将删除文件的最后一行的最后一个字符。
答案 1 :(得分:1)
这是否必须是java问题?对于像这样的基本文件/字符串操作,我更喜欢使用像Perl这样的东西。下面的perl脚本将从文件中删除最后一个字节(或本例中为char)
my $fsize = -s $filename;
# print $size."\n";
open($FILE, "+<", $filename) or die $!;
seek $FILE, $size-2, SEEK_SET;
print $FILE ";";
close $FILE;
答案 2 :(得分:0)
static class CopyFileContent {
public static void main(String[] args) {
String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
File myDir = new File(root + "/MyFolder");//create folder in internal storage
myDir.mkdirs();// make directory
File destFile = new File(myDir, FILENAME11);//making a new file in the folder
/* Source file, from which content will be copied */
File sourceFile1 = new File(myDir, FILENAME12);
File sourceFile2 = new File(myDir, FILENAME13);
File sourceFile3 = new File(myDir, FILENAME14);
/* destination file, where the content to be pasted */
// File destFile = new File(FILENAME);
/* if file not exist then create one */
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream input1 = null;
InputStream input2 = null;
InputStream input3 = null;
OutputStream output = null;
InputStream input4 = null;
try {
/* FileInputStream to read streams */
input1 = new FileInputStream(sourceFile1);
input2 = new FileInputStream(sourceFile2);
input3 = new FileInputStream(sourceFile3);
/* FileOutputStream to write streams */
output = new FileOutputStream(destFile, true);
byte[] buf = new byte[1024];
int bytesRead;
output.write("{Record:[{".getBytes());
while ((bytesRead = input1.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
}
while ((bytesRead = input2.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
}
while ((bytesRead = input3.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
output.write(b.getBytes());
output.write(d.getBytes());
output.write("}]}".getBytes());
output.write("\r\n".getBytes());
}
RandomAccessFile f1=new RandomAccessFile(destFile,"rw");
long length1= f1.length()-1;
f1.setLength(length1);
f1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != input1) {
input1.close();
}
if (null != input2) {
input2.close();
}
if (null != input3) {
input3.close();
}
if (null != output) {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}