String data =
"Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda8 2064208 407212 1552140 21% /
tmpfs 4058672 0 4058672 0% /dev/shm
/dev/sda1 1034096 62636 918932 7% /boot
/dev/sda11 1032088 117684 861976 13% /home
/dev/sda6 6551480 5514296 704384 89% /opt
/dev/sda2 203145268 165930964 26728680 87% /t24bmifs
/dev/sda7 5160576 141484 4756948 3% /tmp
/dev/sda3 15239564 13005132 1460288 90% /usr
/dev/sda9 2064208 68760 1890592 4% /usr/local
/dev/sda10 2064208 1811884 147468 93% /var
/dev/mapper/t24linfs-t24linlv
2113783728 1622849504 383560248 81% /t24linfs
/dev/mapper/oracfsvg-oracfsvl
1909423812 1372203712 440227028 76% /oraclefs"
Below is the code for reference
public void setData(String procText)
{
try
{
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(procText));
Document xDoc = docBuilder.parse(is);
XPath xPath = XPathFactory.newInstance().newXPath();
String status = (String) xPath.evaluate("//status", xDoc, XPathConstants.STRING);
if(status.compareTo("OK")!=0)
{
//show error
}
else
{
String data = (String) xPath.evaluate("//data", xDoc, XPathConstants.STRING);
String[] lines = data.split("\n", 0);
String line = "";
model.addRow(new Object[]{ procData[0], procData[1], procData[2], procData[3], procData[4], procData[5] } );
}
jTable1.setAutoCreateRowSorter(true);
jTable1.setModel(model);
}
catch(Exception ex)
{
System.out.println("Exception - " + ex.getClass().getName()+":"+ ex.getMessage());
}
}
Anyone please help me how can i split this string data. I can able to split first 11 values using split("\\n",0)
. But the last two values, I don't know how to proceed. I will be splitting the string and assign it to a string array and again i will split the string array using space("\\s") and will pass it to an object[] to display it as a table format in a dialog box.
Try this regex ^\\s+([\\w-\\/]+)+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+%\\s+(\\/[\\w-]*)+
It is a bit ugly but it will match each of your lines. You can easily correct it for any exceptional cases you have.
Use it in java like this:
String[] lines = str.split("^\s+([\w-\/]+)+\s+\d+\s+\d+\s+\d+\s+\d+%\s+(\/[\w-]*)+");
Now you should have each of your lines in the lines array.
I tried this and its a bit verbose. But you will have some control over how you can process. For example, the first header line has seven tokens and the rest six. For testing I used the text from the post (as it is) and created a text file in a Windows Notepad.
import java.util.*;
import java.io.*;
public class StringSplitTest {
private static List<String> words = new ArrayList<>();
private static String word = "";
private static boolean wordFlag = true;
public static void main(String [] args) throws IOException {
// Read file and create word tokens
FileReader reader = new FileReader("test.txt");
int c;
while ((c = reader.read()) != -1) {
makeWords((char) c);
}
reader.close();
// Process tokens to get result
int n = 0; // tracks count of words on a line
List<String> line = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
if (i < 7) {
// The first header line has 7 tokens
// ignore for now
continue;
}
// Process remaining lines (6 tokens for each line)
if (++n == 7) {
System.out.println(line); // prints a line
n = 1;
line = new ArrayList<>();
}
line.add(words.get(i));
}
System.out.println(line); // prints last line
}
/*
* Processes all text (a character at a time and stores them as
* word tokens in a List<String>. Uses whitespaces as delimiter.
* NOTE: The whitespace as defined in the Character.isWhitespace()
* https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html.
*/
private static void makeWords(char c) {
if (! Character.isWhitespace(c)) {
if (! wordFlag) {
wordFlag = true;
word = "";
}
word = word + String.valueOf(c);
}
else {
if (wordFlag) {
wordFlag = false;
words.add(word);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.