JAVA Help : Append file and remove last character

am I still very new to java, only had one semester of it. I have my first internship and it isn't a programming internship, just a general IT internship Since it was only my first semester. My boss does not know Java, nor does anyone in the building. He knew I had some basic programming experience and told me to take a stab at the problem he is having. He has a report that is saved and the very last line, the very last character of the report is a character turn symbol, and we need remove that because it is giving us problems on the website. I am not sure if I am even on the right track, at this point I am just doing trial and error. Please help :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); */ } }

-------------Problems Reply------------

How big are the files? If they are not that large, you can read the entire file into a string and then chop off the last character:

//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);

Then you just need to write withoutLastCharacter out to the file.

Otherwise you need to read in the original file line by line and write it out to a temporary file, and then copy that file over the original one. However, if you are on the last line, you will chop off the last character. Here's some code that should give you an idea of the basic logic:

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
...
}

You also mentioned that it doesn't have to be Java. If you're on Linux or Mac, you can just do this with sed:

sed -i '$s/.$//' <filename>

This will delete the last character of the last line of the file.

Does this have to be a java problem? For something like this where it is basic file/string manipulation I prefer to use something like Perl. The below perl script will delete the last byte (or char in this case) from a file

my $fsize = -s $filename;
# print $size."\n";
open($FILE, "+<", $filename) or die $!;
seek $FILE, $size-2, SEEK_SET;
print $FILE ";";

close $FILE;

Category:java Views:3 Time:2018-11-28
Tags: java

Related post

  • remove all characters up to £ and remove last character ] 2011-07-16

    I have the following option text that I want to put into a variable plus remove all characters except the value after the £ then convert it to a number. In this case 28.00 <option value="294">Set of 2 Boots</option><option value="295"

  • Remove last character from C++ string 2010-02-22

    How can I remove last character from a C++ string? I tried st = substr(st.length()-1); But it didn't work. --------------Solutions------------- For a non-mutating version: st = myString.substr(0, myString.size()-1); Simple solution. Probably O(1) tim

  • Order the lines in a file by the last character on the line 2011-01-25

    Can you please help me with this: I want to build a method in C# which will order a lot of files by the following rule every line contains strings and the last character in every line is an int. I want to order the lines in the file by this last char

  • Find a line in a file and remove it 2009-09-04

    I'm looking for a small code snippet that will find a line in file and remove that line (not content but line) but could not find. So for example I have in a file following: myFile.txt: aaa bbb ccc ddd Need to have a function like this: public void r

  • In the save as process .docx saves turn into .tmp files and removes recent changes, 2013-07-30

    In the save as process filename.docx saves turn into ~wxxxxx.tmp files and removes recent changes, It reverts to a copy of original document; Office 2010 Professional running on Windows 7 Pro 64 bit; (12GB of DDR3 RAM) saving on a local drive (more t

  • Looping through a file and removing certain lines 2013-07-05

    I want to loop through a file and remove certain lines. Example file: test.txt a b c d What I have: FILE=/tmp/test.txt while read FILE do # if the line starts with b delete it otherwise leave it there ? done --------------Solutions------------- This

  • How to open a file and remove the last line? 2010-04-26

    I am looking to open up a file, grab the last line in the file where the line = "?>", which is the closing tag for a php document. Than I am wanting to append data into it and add back in the "?>" to the very last line. I've been trying a few a

  • Bash script: Appending text at the last character of specific line of a file 2011-05-04

    I am trying to append a variable at the last character of a specific line of a file from a bash script. The file is called myfile.txt and what I want to do is to append the contents of a variable named VERSION right after the last character of the li

  • java- read last 2 lines in a file and delete last line, then add 2 fixed string values at end of file 2011-10-07

    I want to read the last 2 lines in some files, and if the content of second last line matches a specific string, then delete the last line only. Also, after the above operation, 2 lines of data have to be appended to the end of the modified file. I s

  • PHP - Remove last character of file 2011-12-02

    I have a little php script that removes the last character of a file. $contents = file_get_contents($path); rtrim($contents); $contents = substr($contents, 0, -1); $fh = fopen($path, 'w') or die("can't open file"); fwrite($fh, $contents); fclose($fh)

  • How can I edit a wav file and remove the last 10 minutes? 2012-03-09

    I have a wav file that is an hour long. I want to remove the last 10 minutes. Can I do this with Windows Media Player? How? Thanks so much, I'm very low tech. --------------Solutions------------- The freeware "Audacity" should be able to do this: Aud

  • Compare many text files that contain duplicate "stubs" from the previous and next file and remove duplicate text automatically 2009-04-06

    I have a large number of text files (1000+) each containing an article from an academic journal. Unfortunately each article's file also contains a "stub" from the end of the previous article (at the beginning) and from the beginning of the next artic

  • Finding duplicate files and removing them 2009-04-14

    well, I am not a Programming guru in python but manage to write some of them :) The one I am writing is finding and removing the duplicate files from the folder. I have multiple copies of mp3 files, and same is the case with other files. So if anyone

  • Objective C - Remove last character from string 2009-07-04

    In Objective C for the iPhone, how would I remove the last character of a string using a button action. --------------Solutions------------- In your controller class, create an action method you will hook the button up to in Interface Builder. Inside

  • Need help accessing files and filders in Windows XP. 2013-03-25

    Ok so I need help with this. 2 weeks ago my main computer which is running windows XP started to have problems. Very soon it wouldnt boot. it shows a BIOS ROM Checksum error and wants me to insert a disc in drive A: I pretty much thought I messed up

  • Windows 7 update corrupted files and removed D drive 2014-08-25

    On 12th October I installed the following updates from windows on my computer; KB2739159 KB2656356 KB915597 KB2731847 KB890830 KB2547666 KB2709981 KB2545698 KB2724197 KB2731771 KB2719857 KB2603229 KB2741355 KB2735855 KB2736233 KB2686831 KB2749655 KB2

  • Why is Java under Program Files and Windows? 2014-09-26

    I have moved on from discussing whether the Java files under the Windows folders are virus/trojan, because I have scanned them and they are the same size as those under the Program Files folders... My question now is WHY does Java appear under both f

  • remove last character in id attribute 2009-11-25

    I am trying to remove the last character from a div id using jQuery. Currently I have: <div id="foo/"></div> But I need: <div id="foo"></div> The character is always a / and is generated automatically (re:annoyingly) by the CM

  • Remove last character of a string (VB.NET 2008) 2011-01-13

    I am trying to remove a last character of a string. This last char. is a newline (system.environment.newline) I have tried some things but I can not remove it. Example: myString.Remove(sFP.Length - 1) Example 2: myString= Replace(myString, Environmen

  • Remove last character if it's a backslash 2011-07-05

    Is there a function to chomp last character in the string if it's some special character? For example, I need to remove backslash if it's there, and do nothing, if not. I know I can do it with regex easily, but wonder if there something like a small

  • Remove last character of NSString 2011-10-03

    I've got some trouble 'ere trying to remove the last character of an NSString. I'm kinda newbie in Objective-C and I have no idea how to make this work. Could you guys light me up? --------------Solutions------------- NSString *newString = [oldString

  • Remove last character from JTextPane 2012-01-07

    I'm writing a vt100 emulator and I'm using a JTextPane with a DefaultStyledDocument to display the formatted text. Now I want to implement the backspace, so i need to be able to remove the last character. I tried the following: doc.remove(doc.getEndP

  • How to find and remove last link tags in a string with php? 2012-01-26

    I have a string like this but number of links can be different for each situation: <a>link 1</a> >> <a>link 2</a> >> <a>link 3</a> >> <a>link 4</a> I want to remove last link in that s

  • Python remove last character of string if its a letter 2012-03-22

    How do I remove the last character of a string if its a letter. e.g if I had the following strings. my_string = 'ABC 1.1 A' my_string2 = 'ABC 1.1 B' my_string3 = 'DEF 1' I would want my_string = 'ABC 1.1', my_string2 = 'ABC 1.1', and my_strin3 = 'DEF

  • Remove last character from wxPython wxTextCtrl? 2012-03-24

    I am trying to remove the very last character from a TextCtrl object in wxPython. I'm using wxPython 2.8.12 and Python 2.7.2. My code is as follows: def btnBkClicked(self, e): self.txtItem.Remove(self, (self.txtItem.GetLastPosition()[-1]), (self.txtI

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.252 (s). 11 q(s)