I already know how to get plain text from the clipboard in Java, but sometimes the text is encoded in some weird DataFlavor, like when copying from Microsoft Word or from a website or even source code from Eclipse.
How to extract pure plain text from these DataFlavors?
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
String data = (String) Toolkit.getDefaultToolkit()
.getSystemClipboard().getData(DataFlavor.stringFlavor);
with the getData() Method and the stringFlavor you should get plain text from the clipboard.
If there are weird text in the clipboard, I think, this should be a problem of the program which puts the data in the clipboard.
You can use following method getting clipboard text in Java:
public String getClipBoard(){
try {
return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedFlavorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
First I haven't worked with clipboard but this seems intresting
From http://docstore.mik.ua/orelly/java/awt/ch16_01.htm
"To read data from the clipboard, a program calls the Transferable.getTransferData() method. If the data is represented by a DataFlavor that doesn't correspond to a Java class (for example, plainTextFlavor), getTransferData() returns an InputStream for you to read the data from."
So if you give it a class which doesn't correspont you get the InputStream and then you can read the "pure" text from the InputStream yourself.
Related
Constructing a String with value as ФФХЧЯЯЯЯэшЩтЯ .The string value is in Russian Language.
String russian=new String("ФФХЧЯЯЯЯэшЩтЯ");
Printing the string as below.
ФФХЧЯЯЯЯ�?шЩтЯ
so the э in the character set is not able to convert.
Tried with all the possible encoding types like, UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3 and many things i have tried
public void setter(String attachment) {
try {
filename=new String(filename.getBytes(),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.filename= filename;
}
I am facing some problem in JSON validation. The code I am using is
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
String stringToValidate="{'customer':[{'condition':'Matches','value':'nordstrom,nordstromrack,neimanmarcus,neimanmarcuslastcall,tjmaxx,macys,dsw,saksfifthavenue,off5th,bananarepublic,jcrew,bloomingdales'}],'notcustomer':[{'condition':'Matches','value':'neimanmarcuslastcall'}],'daterange':[{'condition':'In the last','value':'6 months'}]}";
try {
System.out.println(stringToValidate);
JSONObject.fromObject(stringToValidate);
/* try {
Map<String, Object> tsMap = new Json2Java().getMap(stringToValidate);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}*/
System.out.println(true);
} catch (JSONException je) {
je.printStackTrace();
System.out.println(false);
}
and it is validating the syntax fine. As you can see I am using single commas (') for my tag names like 'customer':...... So my problem is, if I remove this ' from the tag still it's printing true and I want to fail the condition in this case.
I have this code that reads an mp3 file
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound {
public static void main(String[] args) {
File sampleFile = new File("test.mp3");
try {
AudioSystem.getAudioFileFormat(sampleFile);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The problem here is that it is returning file not supported exception, the file here is an mp3 file. Java doesn't support mp3 files? if so what are others to validate an audio file?(like ogg, wav)
You may take a look at Apache Tika library. It can detect type of a file by its content and extract file metadata. It supports mp3 format.
Here is an example of file type detection with Apache Tika.
You need to add MP3SPI library so that java audio api could recognize and decode mp3 files.
Could anybody help me, please? I have this code:
Process a;
try {
a = Runtime.getRuntime().exec("su");
DataOutputStream swp = new DataOutputStream(a.getOutputStream());
swp.writeBytes("cat /proc/sys/vm/blabla\n");
swp.writeBytes("exit\n");
swp.flush();
try {
a.waitFor();
if (a.exitValue() != 255) {
// TODO Code to run on success
toastMessage("root");
}
else {
// TODO Code to run on unsuccessful
toastMessage("not root");
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
toastMessage("not root");
}
} catch (IOException e) {
// TODO Code to run in input/output exception
toastMessage("not root");
}
This code swp.writeBytes("cat /proc/sys/vm/blabla\n"); copies text (string) from system file called "blabla". And then I need to put this text (String) to my SharedPreferences. How can I achieve it?
If this was your code, you should know. In case you don't, you can follow the Tutorial.
In your case, you'll want to use the putString()-method.
To get what cat printed out, you'll need the output of your Process-object. Use the getInputStream()-method.
How can I make a hyperlink in a JFace Dialog that when clicked opens the link in the default web browser. A full example would be useful. I know there is a org.eclipse.jface.text.hyperlink package but I can't find a suitable example.
Are you running an RCP application?
If so, then the following code will open your link in the default OS browser:
// 'parent' is assumed to be an SWT composite
Link link = new Link(parent, SWT.NONE);
String message = "This is a link to Google";
link.setText(message);
link.setSize(400, 100);
link.addSelectionListener(new SelectionAdapter(){
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("You have selected: "+e.text);
try {
// Open default external browser
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(e.text));
}
catch (PartInitException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
});
The above assumes that you do not want to scan existing text for hyperlinks but simply wish to create one programmatically. If the former is required then you'll need to use the API from JFace text packages or suchlike.