my codes dont seem to properly address what i intend to achieve.
a long string instead of a well broken and seperated string
it does not handle the 'seperator' appropriately ( produces , instead of ",")
also the 'optional' ( produces ' instead of " '")
Current result:
LOAD DATA INFILE 'max.csv'BADFILE 'max.bad'DISCARDFILE
'max.dis' APPEND INTO TABLEADDRESSfields terminated by,optionally enclosed
by'(ID,Name,sex)
the intended result should look like this
is there a better way of doing this or improving the above codes
Yeah. Use the character \n to start a new line in the file, and escape " characters as \". Also, you'll want to add a space after each variable.
content = " LOAD DATA\nINFILE "+ fileName + " BADFILE "+ badName + " DISCARDFILE " +
discardName + "\n\nAPPEND\nINTO TABLE "+ table + "\n fields terminated by \"" + separator
+ "\" optionally enclosed by '" + optional + "'\n (" + column + ")";
This is assuming fileName, badName, and discardName include the quotes around the names.
Don't reinvent the wheel... the apache commons-io library does all that in one line:
FileUtils.write(new File(controlName), content);
Here's the javadoc for FileUtils.write(File, CharSequence):
Writes a CharSequence to a file creating the file if it does not exist
To insert a new line you need to use \n or \r\n for windows
for example
discardName + "\n" //New line here
"APPEND INTO TABLE"
For the double quote symbol on the other hand you need to specifically type \" around the comma:
"fields terminated by \"" + separator +"\""
which will produce this ","
and that is similar to what the optional variable needs to be
Related
I have a huge xml as follows which I have to assign to a single string. This xml is really huge to so I have to go every line and put " " and + before each line. How can I do that with eclipse's find/replace using regEx.
<Student>
<name></name>
<age></age>
<class></class>
<section></section>
// More tags
</Student>
What I want :
public final String studentRequest = "<Student>"
+ " <name></name> "
+ " <age></age> "
+ " <class></class>"
+ " <section></section> "
+ " </Student>" ;
Thanks in advance.
Highlight the lines with the text, and remember to put the Scope to "Selected Lines".
Then you search for (.)$ and replace by $1"\+ (or $1\\n"\+ if you need linebreaks), and replace ^(.) by "$1.
This will turn
hello
world
!
into
"hello"+
"world"+
"!"
Add a variable assignment at the first line and a semicolon at the end, and you are done. There might be issues if you text contains special characters, though so try to clean those up first.
I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s+]");
System.out.println("spart[0]: " + spart[0]);
System.out.println("spart[1]: " + spart[1]);
System.out.println("spart[2]: " + spart[2]);
System.out.println("spart[3]: " + spart[3]);
I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s+]");
System.out.println("spart[0]: " + spart[0]);
System.out.println("spart[1]: " + spart[1]);
System.out.println("spart[2]: " + spart[2]);
System.out.println("spart[3]: " + spart[3]);
But, I am getting some elements which are blank. The output is:
spart[0]: s
spart[1]: film
spart[2]:
spart[3]: normal
- is a special character in PHP character classes. For instance, [a-z] matches all chars from a to z inclusive. Note that you've got )-_ in your regex.
- defines a range in regular expressions as used by String.split argument so that needs to be escaped
String[] part = line.toLowerCase().split("[,/?:;\"{}()\\-_+*=|<>!`~##$%^&]");
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s]+");
When writing to a text file in java , how do I enter values into a new line
code snippet
while (rs.next()) {
int sport = rs.getInt("sport");
String name = rs.getString("name");
out.write(sport + " : " + name);}
the text file populates " value1 value2 value3...etc"
I want it to populate
value1
value2
value3
.
If 'out' is a PrintWriter, use println().
If 'out' is a BufferedWriter, use newLine().
If 'out' is some other Writer, use write('\n'), or append the newLine directly to the string you're writing. If you want the system's line separator, see System.getProperty() with the value "line.separator".
Very simple
out.write(sport + " : " + name + "\n");
That's all.
use out.write(10); to add new line. 10 is acsii character for newline. But it is not work for Indirect/Direct Buffer type FileChannel.
I'm writing some code for web services for my Android app which uses JSON. The url should look like this
url = url + "?maddr=" + mailAddr + "&pwd=FB&lect=" + """ + lectName + """ + "&fb=Test";
This is because the Lectname may be two or more words. However the compiler wont accept """, is there a character I can precede the " with to get the compiler to accept it into my string?
Try " \" ". You have to escape the "
http://en.wikipedia.org/wiki/Escape_character
You need this in (nearly) every programming language.
Java newbie here, I'm having trouble setting a new line in this code:
String FnameTextboxText = FnameTextbox.getText();
String LastnameTextboxText = LastnameTextbox.getText();
String CourseTextboxText = CourseTextbox.getText();
Summary.setText("Firstname:" + " " + FnameTextboxText + "\nLastname:" + " " + LastnameTextboxText + "\nCourse:" + " " + CourseTextboxText);
Also tried something like: "\n" + "Lastname" But its no good.
Do you have any idea on how to make new lines. So that it'll look like this;
Firstname: x
Lastname: y
Course: Z
Using netbeans 6.8. On windows.
I guess you need to use TextArea.
First, use TextArea
Second, test using \r or \n or \r\n
Sometimes, people use \n to make new line and sometimes, like me, use \r\n to make new line